我正在玩Rails 4.x beta并尝试使用carrierwave获取嵌套属性.不确定我正在做的是正确的方向.搜索后,最后查看导轨源和强参数,我发现了下面的注释.
Run Code Online (Sandbox Code Playgroud)# Note that if you use +permit+ in a key that points to a hash, # it won't allow all the hash. You also need to specify which # attributes inside the hash should be whitelisted.
所以它说你必须在has中指定每一个单独的属性,我尝试了以下内容:
帕拉姆的例子:
{"utf8"=>"?",
"authenticity_token"=>"Tm54+v9DYdBtWJ7qPERWzdEBkWnDQfuAQrfT9UE8VD=",
"screenshot"=>{
"title"=>"afs",
"assets_attributes"=>{
"0"=>{
"filename"=>#<ActionDispatch::Http::UploadedFile:0x00000004edbe40
@tempfile=#<File:/tmp/RackMultipart20130123-18328-navggd>,
@original_filename="EK000005.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"screenshot[assets_attributes][0][filename]\"; filename=\"EK000005.JPG\"\r\nContent-Type: image/jpeg\r\n">
}
}
},
"commit"=>"Create Screenshot"}
Run Code Online (Sandbox Code Playgroud)
调节器
def screenshot_params
params.require(:screenshot).permit(:title,
:assets_attributes => [:filename => [:@tempfile,:@original_filename,:@content_type,:@headers]
Run Code Online (Sandbox Code Playgroud)
以上不是"工作"(它不触发载波)但是当我使用我发现的标准嵌套示例时,我不再收到错误(未经许可的参数:文件名):
def screenshot_params
params.require(:screenshot).permit(:title, assets_attributes: :filename)
Run Code Online (Sandbox Code Playgroud)
如果有人能提供帮助就会很棒.我无法找到嵌套了一个指向哈希的键的示例.
关于这个主题至少有10个问题,但没有一个回答这个问题.许多问题都与Rails的形式像这样,我没有,或者是更复杂的,像JSON结构,这还是这个.
编辑关于已接受的答案以及为什么这不是完全相同的
来自@CarlosRoque的答案中的链接问题最初看起来是同样的问题,但它只解决了这个特定问题的Rails方面.
如果您阅读了所有注释,您将看到多次尝试更改template_params方法,使用"template_items_attributes"对嵌套属性"template_items"进行RENAME或REPLACE.这是必要的,因为Rails accepts_nested_attributes_for需要将"_attributes"附加到名称,否则它无法看到它.
如果你检查修复wrap_parameters的答案中的猴子补丁代码,以便它适用于嵌套属性,你仍然会遇到它实际上找不到"template_items"(嵌套对象)的问题,因为它没有后缀"_attributes ".
因此,要完全解决此问题,还必须修改客户端以将嵌套对象作为"template_items_attributes"发送.对于JS客户端,可以通过在对象上实现toJSON()方法来完成,以便在序列化期间对其进行修改(此处为示例).但请注意,当您反序列化JSON时,您需要手动创建该对象的实例以使toJSON()起作用(在此解释原因).
我有一个简单的has_many/belongs_to:
楷模:
class Template < ApplicationRecord
belongs_to :account
has_many :template_items
accepts_nested_attributes_for :template_items, allow_destroy: true
end
class TemplateItem < ApplicationRecord
belongs_to :template
validates_presence_of :template
enum item_type: {item: 0, heading: 1}
end
Run Code Online (Sandbox Code Playgroud)
从客户端发送的json看起来像这样:
{
"id": "55e27eb7-1151-439d-87b7-2eba07f3e1f7",
"account_id": "a61151b8-deed-4efa-8cad-da1b143196c9",
"name": "Test",
"info": "INFO1234",
"title": "TITLE1",
"template_items": [
{
"is_completed": false,
"item_type": "item"
},
{
"is_completed": false,
"item_type": "heading"
}
]
} …Run Code Online (Sandbox Code Playgroud) 这个问题部分回答了我的问题.作者使用类似的json结构..
我的问题:如何在嵌套对象中允许嵌套数组?我有一个Contribution模特has_many Features.我正在尝试创建GeoJSON多边形.在coordinates保持为空
这是我发送的JSON
{
"contribution": {
"features_attributes": [
{
"geojson": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
7.263336181640625,
52.07190953840937
],
[
7.263336181640625,
52.135173926548894
],
[
7.404785156249999,
52.135173926548894
],
[
7.404785156249999,
52.07190953840937
],
[
7.263336181640625,
52.07190953840937
]
]
]
}
}
}
],
"title": "324",
"description": "23"
}
}
Run Code Online (Sandbox Code Playgroud)
目前我的许可证代码如下:
params.require(:contribution).permit(
:title,
:description,
features_attributes: [
{ geojson: [
:type,
{ geometry: [
:type,
#{ coordinates: [] } …Run Code Online (Sandbox Code Playgroud) 我在Rails 4中遇到了一个复杂的问题,我将在下面介绍.我使用简单的形式和awesome_nested_fields宝石.
我的应用程序中有一些带有一些字段的事件
这是我在working_nested_attributes实现之前的工作event_controller参数:
private
def event_params
params.require(:event).permit(:title, :description, :type_id, :price, :program,
:start_date, :end_date, :image, category_ids: [])
end
Run Code Online (Sandbox Code Playgroud)
现在我想在我的活动中添加一些发言者,并让用户决定每个活动他想要多少个发言者.因此,根据他们的文档,我添加了嵌套字段gem并使扬声器成为嵌套字段.
Event.rb
class Event < ActiveRecord::Base
has_many :speakers
accepts_nested_attributes_for :speakers, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
Speaker.rb
class Speaker < ActiveRecord::Base
belongs_to :event
end
Run Code Online (Sandbox Code Playgroud)
添加扬声器(在我的添加事件simple_form_for中):
<%= f.nested_fields_for :speakers do |f| %>
<fieldset class="item">
<%= f.label :name %>
<%= f.text_field :name %>
<a href="#" class="remove">remove</a>
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
</fieldset>
<% end %>
Run Code Online (Sandbox Code Playgroud)
更新强参数的控制器:
private
def event_params
params.require(:event).permit(:title, :description, :type_id, :price, …Run Code Online (Sandbox Code Playgroud) Rails 4:我来自rails 3.2.x我有一个问题.如何在没有控制器的情况下使用Strong参数.
我有这个型号:
Track (the only one that has a Controller )
has_many :tracksegments, :dependent => :destroy
has_many :points, :through => :tracksegments
Tracksegment
belongs_to :track
has_many :points, :dependent => :destroy
points
belongs_to :tracksegment
Run Code Online (Sandbox Code Playgroud)
Track是唯一一个拥有Controller的轨道,因此它具有一些强参数.
我想知道在哪里可以放置属于"tracksegment"和"points"的参数在Rails 3.x中它直接在模型中但在rails 4中我没有控制器.
我正在开发一个带有名为 element 的 atter_accessor 对象的模型。我想将表单数据数组传递给元素对象。在 Rails 控制台中,我收到“未经允许的参数”错误。
Parameters: {"authenticity_token"=>"[FILTERED]", "category"=>{"name"=>"asfd", "body"=>"asf", "element"=>{"1"=>"asfd:text", "2"=>"asfd:text", "3"=>"asfd:text"}}, "type"=>"text", "commit"=>"Create Category"}
Unpermitted parameter: :element. Context: { controller: CategoriesController, action: create, request: #<ActionDispatch::Request:0x0000000106b3ff68>, params: {"authenticity_token"=>"[FILTERED]", "category"=>{"name"=>"asfd", "body"=>"asf", "element"=>{"1"=>"asfd:text", "2"=>"asfd:text", "3"=>"asfd:text"}}, "type"=>"text", "commit"=>"Create Category", "controller"=>"categories", "action"=>"create"} }
Run Code Online (Sandbox Code Playgroud)
模型中 attr_accessor :elements
在控制器中
def category_params
params.require(:category).permit(:name, :body, :elements => [])
end
Run Code Online (Sandbox Code Playgroud)
我尝试了很多替代方案,也将其更改:elements为element: [],但没有任何效果。我想我在这里遗漏了一些东西,这就是我得到未经允许的参数的原因。
我在编辑嵌套属性时遇到问题。我收到此错误:
no implicit conversion of Symbol into Integer
Run Code Online (Sandbox Code Playgroud)
事件.rb:
Class Event < ActiveRecord::Base
has_many :event_joins, :dependent => :destroy
accepts_nested_attributes_for :event_joins
end
Run Code Online (Sandbox Code Playgroud)
events_controller.rb :
private
def event_params
params.require(:event).permit(event_joins_attributes: [:duration])
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb :
=f.fields_for :event_joins_attributes do |a|
=number_field_tag 'event[event_joins_attributes][duration]'
end
Run Code Online (Sandbox Code Playgroud)
如果我改变了我params之前的许可
params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
no implicit conversion of String into Integer
Run Code Online (Sandbox Code Playgroud)
我已经阅读了很多关于大规模分配的嵌套属性的帖子,但没有任何效果。这是我读过的部分帖子。
rails-4-strong-parameters-nested-objects
当然,我不想做
params.require(:event).permit!
Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4