草庐IT

JSON_FACTORY

全部标签

ruby-on-rails - will_paginate JSON 支持?

我想知道是否有人可以告诉我will_paginate是否可以开箱即用地支持JSON,或者是否必须对其进行破解?我想将页面数据添加到JSON响应中,而will_paginate管理分页。 最佳答案 内容如下:@posts=Post.paginate:page=>params[:page]respond_todo|format|format.json{render:json=>{:current_page=>@posts.current_page,:per_page=>@posts.per_page,:total_entries=>@p

ruby-on-rails - 名称错误 : uninitialized constant Factory

我正在关注thistutorial为了与工厂女孩、rspec一起开始使用TDDonrails,我遇到了这个问题,我无法理解。这是我的“工厂”.rb(events.rb)require'faker'FactoryGirl.definedofactory:eventdoname"HIGH"genre"house,techno,idb"venue_name"WestbourneStudios"venue_address"4-6ChamberlayneRoad"venue_postcode"NW103JD"begin_time"10pm"end_time"2am"user_id2descrip

Ruby:使用 JSON 正文 PUT 请求?

我需要使用ruby​​创建一个HTTPPUT请求。请求有一个JSON正文我能够使用以下方法生成JSON正文:require'rubygems'require'json'jsonbody=JSON.generate["message"=>"test","user"=>"user1"]我需要将此PUT请求发送到url:require'open-uri'url=URI.parse('http://www.data.com?access_token=123')谁能告诉我如何在Ruby中做到这一点? 最佳答案 像这样使用restclient(

ruby - 在 ruby​​ 中将 .json 转换为 .csv

我想使用ruby​​将.json文件转换为.csv文件。请帮我做这件事。同时提出实现此目的的任何工具。 最佳答案 尝试这样的事情:require'csv'require'json'csv_string=CSV.generatedo|csv|JSON.parse(File.open("foo.json").read).eachdo|hash|csv 关于ruby-在ruby​​中将.json转换为.csv,我们在StackOverflow上找到一个类似的问题:

ruby-on-rails - 如何在 ruby​​ in rails 中创建复杂的 Json 响应

我正在做ruby​​onrails项目,我想添加对Json的响应。一个简单的方法是:--defindex@users=User.allrespond_todo|format|format.html#index.html.erbformat.xml{render:xml=>@users}format.json{render:json=>@users.to_json}endend但是这有一些问题:-我不想在json响应中提供整个用户对象,例如密码哈希和缓存计数器属性。Facebook、twitter属性等我想在json对象中添加更多详细信息(考虑到stackoverflow模型),例如每个

ruby-on-rails - Ruby on Rails 高级 JSON 序列化

我希望在我的Rails应用程序中通过JSON呈现所有文章的索引以及完整的文章,但我在弄清楚如何去做时遇到了一些麻烦。现在这是我的Controller:ifparams[:id]@article=Article.find(params[:id])else@article=Article.published.not_draft.by_recent.firstendrespond_todo|format|format.js{render:json=>@article.to_json(:except=>[:created_at,:updated_at,:draft,:id,:publish],

ruby-on-rails - Rails JSON API 的基于 token 的身份验证

我在Rails中制作API。对于普通身份验证,我们使用设计,但在API中如何实现身份验证设计。gem'devise_token_auth'有人喜欢这个gem用于身份验证,但没有可用的教程。如何在RailsAPI中实现身份验证? 最佳答案 你能做的最好的事情就是跟随githubtutorials哪些最有可能是最新的。首先你应该遵循TLDR部分。请注意,前端开发人员需要了解usagespecification。.最后,您想阅读文档。以下是一些可能有帮助的示例:路线Rails.application.routes.drawdo#Stuff

ruby-on-rails - factory_girl + rspec 似乎不会在每个示例后回滚更改

类似于此处描述的问题:http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec简而言之(缩短的代码):规范助手:config.use_transactional_fixtures=trueconfig.use_instantiated_fixtures=false工厂.rb:Factory.define:statedof.name"NY"end在我的规范中before(:each)do@static_model=Factory(:state)#withvalidateuniquenessofstatenameend错

ruby-on-rails - 将 Ruby 哈希转换为 JSON(无转义字符)

我有一个哈希:my_hash={"bob.johnson@example.com"=>{"first"=>"Bob","last"=>"Johnson"},"lisa.dell@example.com"=>{"first"=>"Lisa","last"=>"Dell"}}当我尝试用my_hash.to_json序列化它时,这是我得到的:"{\"bob.johnson@example.com\":{\"first\":\"Bob\",\"last\":\"Johnson\"},\"lisa.dell@example.com\":{\"first\":\"Lisa\",\"last\":\

ruby-on-rails - Ruby JSON.pretty_generate ... 很不漂亮

我似乎无法让JSON.pretty_generate()在Rails中实际生成漂亮的输出。我正在使用Rails2.3.5,它似乎会自动加载JSONgem。惊人的。在使用script/console时,这确实会产生JSON:some_data={'foo'=>1,'bar'=>20,'cow'=>[1,2,3,4],'moo'=>{'dog'=>'woof','cat'=>'meow'}}some_data.to_json=>"{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":