Tag: json

Rails 3 +设计 – 如何设计回应JSON

我的环境是带有Devise 1.1的Rails 3.0.1。 我正在开发一个移动Web应用程序,主要使用javascript,并且希望保留尽可能多的基于JSON的通信。 有没有办法让设计用JSON响应成功/失败消息,而不是必须遵循302重定向并解析HTML? 看着用这个 。 ……但是没有它工作。

如何使用rails remote:true参数和JSON?

在使用rails remote:true参数提交ajax请求时,如何指定要返回JSON对象? 一旦返回JSON对象,我如何捕获并使用它?

在Rails 4中保存嵌套模型

有点像Rails的新东西,有点像。 其中一个模型依赖于has_many / belongs_to关联中的另一个。 基本上,在我的应用程序上创建“发布”时,用户还可以附加“图像”。 理想情况下,这是两个独立的模型。 当用户选择照片时,一些JavaScript会将其上传到Cloudinary,并且返回的数据(ID,宽度,高度等)将被JSON字符串化并设置在隐藏字段上。 # The HTML = f.hidden_field :images, :multiple => true, :class => “image-data” # Set our image data on the hidden field to be parsed by the server $(“.image-data”).val JSON.stringify(images) 当然,这种关系存在于我的Post模型中 has_many :images, :dependent => :destroy accepts_nested_attributes_for :images 和我的图像模型 belongs_to :post 我丢失的地方是如何处理Post控制器的创建方法中的序列化图像数据? 简单地解析JSON并保存它不会在保存时创建带有数据的Image模型(并且感觉不对): params[:post][:images] = JSON.parse(params[:post][:images]) 所有这些基本上都达到了以下参数: {“post”: {“title”: […]

JSON包括语法

我的设置:Rails 2.3.10,Ruby 1.8.7 我在几个模型之间有一套相当复杂的关系。 class A has_many :classB has_many :classD end class B belongs_to :classA has_many :classC end class C belongs_to :classB belongs_to :classE end class D belongs_to :classA belongs_to :classE end class E has_many :classD has_many :classC end 我遇到了JSON语法的问题,以获取以classA开头的所有相关信息。 这是我到目前为止所做的工作。 classA.to_json(:include => {:classB => {:include => [:classC, :classE]}}) 我无法使语法工作也包括classD和相关的classE记录。 更新实际上这样的东西可能会起作用,除了我不能混合哈希和数组 classA.to_json(:include => [ :classB […]

to_json在Rails中返回字符串而不是json

下面的代码应该返回一个json: @series = @series.map do |serie| { :name => serie.name, :id => serie.id } @series.to_json 它的回报如下: “[{\”name\”:\”Barra\”,\”id\”:3},{\”name\”:\”Botafogo 1\”,\”id\”:1},{\”name\”:\”Botafogo 2\”,\”id\”:2},{\”name\”:\”Tijuca\”,\”id\”:4}]” 为什么它返回一个字符串而不是一个json? ‘json’gem在我的.rb文件的顶部都安装并需要

通过json设计失败认证发回html而不是json

我已设法设置json身份validation。 我实现了以下代码: class Users:: SessionsController resource_name, :recall => “#{controller_path}#failure”) render :json => {:success => true} } end end def destroy respond_to do |format| format.html {super} format.json { Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) render :json => {} } end end def failure render :json => {:success => false, :errors => [“Login Failed”]} end end 这很好,但是当身份validation失败时,失败并不会返回json失败。 我有一个定制的设计失败。 […]

如何在rails中自定义json输出?

我有一个语言模型,我希望将所有语言作为json,但json输出如下所示 [{ “语言”:{ “created_at”:空, “ID”:1, “语言”: “英语”, “的updated_at”:空}},{ “语言”:{ “created_at”:空, “ID” 2, “语言”: “瑞典”, “的updated_at”:空}},{ “语言”:{ “created_at”:空, “ID”:3, “语言”: “德国”, “的updated_at”:空}},{ “语言”:{ “created_at”:空, “ID”:4, “语言”: “法国”, “的updated_at”:空}},{ “语言”:{ “created_at”:空” ID “:5,” 语言 “:” 西class牙语”, “的updated_at”:空}},{ “语言”:{ “created_at”:空, “ID”:6, “语言”: “荷兰人”, “的updated_at” :空}},{ “语言”:{ “created_at”: “2012-12-03T05:01:18Z”, “ID”:7, “语言”: “泰米尔语”, “的updated_at”:“2012-12- 03T05:01:18Z“}}] 但我想这样做 { “语言”:[{ “created_at”:空, “ID”:1, […]

从JSON反序列化ActiveRecord

我想使用JSON序列化将查询结果保存到redis中并查询它。 将查询结果提供给json非常简单: JSON.generate(Model.all.collect {|item| item.attributes}) 但是我没有找到将其反序列化回ActiveRecord的正确方法。 最直接的方式: JSON.parse(@json_string).collect {|item| Model.new.from_json(item)} 给我一个错误: WARNING: Can’t mass-assign protected attributes: id 所以id变空了。 我想过只使用OpenStruct代替ActiveRecord,但我相信有更好的方法。

使用rails中的simple_form和AJAX解析JSON错误

我有一个表单在用户仪表板上使用Simple Forms和remote => true(由仪表板视图和仪表板控制器处理)。 该表单允许用户添加自定义游戏规则; 但是,它通过Jquery的.load()函数加载表单。 表单从“规则”视图加载(由规则控制器处理)。 当用户单击提交按钮时,我没有得到任何错误validation。 她是规则控制器上的创建操作: def create @rule = Rule.new(params[:rule]) respond_to do |format| if @rule.save format.html { redirect_to dashboard_path, notice: ‘Rule was successfully created.’ } format.json { render json: @rule, status: :created, location: @rule } format.js else format.html { render template: ‘dashboard/index’} format.json { render json: @rule.errors, status: :unprocessable_entity } format.js […]

ActiveRecords到JSON的数组

我知道ActiveRecord提供了一个to_json方法,它允许使用:only和:except从JSON输出中过滤掉字段。 目前我正在使用以下格式从find作为JSON格式化数组: @customers = Customer.find(:all) … format.js { render :json => @customers} 我如何能够在数组中的对象中选择要输出的字段? 有快捷方式还是需要手工完成? 干杯,亚当