Ruby on rails 嵌套参数的奇怪行为
我在嵌套参数中遇到了这种奇怪的行为。 当我试图保存表单时,它会显示Ruby on rails 嵌套参数的奇怪行为,ruby-on-rails,ruby-on-rails-4,nested-attributes,Ruby On Rails,Ruby On Rails 4,Nested Attributes,我在嵌套参数中遇到了这种奇怪的行为。 当我试图保存表单时,它会显示Unpermitted参数:organization\u type 对于我的组织类型,我只有一个模型,但我认为这不应该是问题所在,因为根据我的理解,属性是在用户控制器中处理的 我试图在表单和控制器白名单中都将属性设置为organization_type(singular),但这不起作用 但是,在表单中,如果我有:organization\u types,该字段将不会显示 我真的很困惑 因此,简要回顾一下: 我没有Organiza
Unpermitted参数:organization\u type
对于我的组织类型,我只有一个模型,但我认为这不应该是问题所在,因为根据我的理解,属性是在用户控制器中处理的
我试图在表单和控制器白名单中都将属性设置为organization_type(singular),但这不起作用
但是,在表单中,如果我有:organization\u types
,该字段将不会显示
我真的很困惑
因此,简要回顾一下:
- 我没有OrganizationType的控制器
- 在我的表格中有:
。如果我把它多元化,它就不会表现出来:组织类型
- 在控制器白名单中,我有:
:组织\类型\属性
- 在用户模型中,我有:Has\u many和accepts\u嵌套为
:organization\u types\u attributes
class User < ActiveRecord::Base
has_many :events
has_many :organization_types
accepts_nested_attributes_for :organization_types
end
class用户
组织类型模型
class OrganizationType < ActiveRecord::Base
belongs_to :user
ORG_TYPES = ['health', 'non-profit', 'foo', 'bar']
end
类组织类型
用户控制器
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
...
def user_params
params.require(:user).permit(:name, ..., organization_types_attributes: [:id, :user_id, :org_type, '_destroy'])
end
class UsersController
用户表单
<%= form_for(@user) do |f| %>
...
<div class="field">
<%= f.label :organization_type %><br>
<%= f.fields_for :organization_type do |builder| %>
<%= builder.select :org_type, options_for_select(OrganizationType::ORG_TYPES) %><br/>
<% end %>
</div>
<% end %>
...
它应该是嵌套形式的:组织类型
:
<%= f.fields_for :organization_types do |builder| %>
<%= builder.select :org_type, options_for_select(OrganizationType::ORG_TYPES) %><br/>
<% end %>
看看这是否有帮助……我把我的型号改成了has\u one。。。并且按照@Ren提到的文档进行操作,现在它似乎正在工作。我注意到的两件事是。1.“我的选择助手”未显示上次保存的。我试过这个
@user.organization\u type.org\u type)%>
-2。它不是更新记录,而是创建一个新记录
def new
@user = User.new
2.times { @user.organization_types.build}
end