我有一个非常简单的问题.但到目前为止还没有找到解决方案.
所以这是我发送给服务器的JSON字符串:
{
"name" : "abc",
"groundtruth" : {
"type" : "Point",
"coordinates" : [ 2.4, 6 ]
}
}
Run Code Online (Sandbox Code Playgroud)
使用新的许可方法,我得到:
params.require(:measurement).permit(:name, :groundtruth)
Run Code Online (Sandbox Code Playgroud)
这不会引发任何错误,但创建的数据库条目包含null而不是groundtruth值.
如果我只是设置:
params.require(:measurement).permit!
Run Code Online (Sandbox Code Playgroud)
所有东西都按预期保存,但当然,这会破坏强参数提供的安全性.
我找到了解决方案,如何允许数组,但没有找到使用嵌套对象的单个示例.这必须是某种可能的,因为它应该是一个非常常见的用例.那么它是怎样工作的?
ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4
我已经搜索了相关的问题,但仍然有问题从我的AngularJS前端返回的rails 4到JSON中更新嵌套属性.
问题:下面的代码概述了从我的Rails4应用程序中从AngularJS传递到Candidate模型的JSON.Candidate模型有很多Works,我试图通过Candidate模型更新Works模型.由于某种原因,Works模型无法更新,我希望有人可以指出我缺少的东西.谢谢你的帮助.
这是候选人AngularJS前端的json:
{"id"=>"13", "nickname"=>"New Candidate", "works_attributes"=>[
{"title"=>"Financial Analyst", "description"=>"I did things"},
{"title"=>"Accountant", "description"=>"I did more things"}]}
Run Code Online (Sandbox Code Playgroud)
然后,Rails通过添加候选标头将此JSON转换为以下内容,但不包括候选标头下的嵌套属性,并且无法通过候选模型更新works_attributes:
{"id"=>"13", "nickname"=>"New Candidate", "works_attributes"=>[
{"title"=>"Financial Analyst", "description"=>"I did things"},
{"title"=>"Accountant", "description"=>"I did more things"}],
"candidate"=>{"id"=>"13", "nickname"=>"New Candidate"}}
Run Code Online (Sandbox Code Playgroud)
candidate_controller.rb包含一个简单的更新:
class CandidatesController < ApplicationController
before_filter :authenticate_user!
respond_to :json
def update
respond_with Candidate.update(params[:id], candidate_params)
end
private
def candidate_params
params.require(:candidate).permit(:nickname,
works_attributes: [:id, :title, :description])
end
end
Run Code Online (Sandbox Code Playgroud)
candidate.rb模型包含以下代码,用于定义与工作模型的has_many关系:
class Candidate < ActiveRecord::Base
## Model Relationships
belongs_to :users
has_many :works, :dependent => :destroy
## …Run Code Online (Sandbox Code Playgroud) json ruby-on-rails nested-attributes ruby-on-rails-3 ruby-on-rails-4
关于嵌套参数有很多问题,但我似乎无法找到解决我特定的简单情况的问题.
我正在尝试允许不是数组的嵌套哈希.我希望这可行:
params.require(:book).permit(:title, :description, style: {:font, :color})
Run Code Online (Sandbox Code Playgroud)
但它导致语法错误.
然而,这有效:
params.require(:book).permit(:title, :description, style: [:font, :color])
Run Code Online (Sandbox Code Playgroud)
但我的问题是,它似乎允许style值为具有属性的项目数组:font和:color.我只想允许具有这两个属性的单个哈希.
我尝试了其他变体,但我不断收到语法错误.我很感激任何帮助.
上下文:Rails 4.1.7,Ruby 2.0.0(它在我的todo列表中进行升级!),而不是使用ActiveRecord.
我有一个带有嵌套fields_for的表单.我试图允许由该嵌套表单生成的参数,但它们被强参数阻止.我使用rails 4.2.4和ruby 2.2.2
我读过一些官方文档:
我已经阅读了相关的SO帖子:
我读过各种博文:
我想我正在按照他们的意思去做,但是我的嵌套属性被强参数拒绝了.我Unpermitted parameters: __template_row__, 0, 1, 2, 3在日志中得到的东西.
这是我的代码:
车型/ enclosure.rb
class Enclosure < ActiveRecord::Base
has_many :resident_animals, -> { order("year DESC, month DESC") }, dependent: :restrict_with_error
validates :name, presence: true, uniqueness: {case_sensitive: false}
accepts_nested_attributes_for :resident_animals, allow_destroy: true, reject_if: :all_blank
def to_s
name
end
end
Run Code Online (Sandbox Code Playgroud)
车型/ resident_animal.rb
class ResidentAnimal < ActiveRecord::Base
belongs_to :enclosure
validates_presence_of :enclosure, :year, :month, :color
...
end
Run Code Online (Sandbox Code Playgroud)
控制器/ enclosures_controller.rb
class EnclosuresController < ApplicationController …Run Code Online (Sandbox Code Playgroud)