Rails - 如何在创建自引用模型时建立关联?
Rails - How to make an association on create for self referenced model?
我有一个 self_referenced 模型(类别):
class Category < ApplicationRecord
has_many :sub_themes_association, class_name: "SubTheme"
has_many :sub_themes, through: :sub_themes_association, source: :sub_theme
has_many :inverse_sub_themes_association, class_name: "SubTheme", foreign_key: "sub_theme_id"
has_many :inverse_sub_themes, through: :inverse_sub_themes_association, source: :category
accepts_nested_attributes_for :sub_themes
end
我想用他的 sub_themes 协会以相同的形式创建一个类别模型。
例如数字 => 类别和 SEO 和编码 => sub_themes
class Admin::CategoriesController < AdminController
def index
@categories = Category.all
end
def new
@category = Category.new
@sub_themes = @category.sub_themes.build
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to admin_categories_path
else
render :new
end
end
private
def category_params
params.require(:category).permit(
:name,
sub_theme_attributes: [
sub_theme_ids:[]
]
)
end
end
和表格:
<%= simple_form_for [:admin,@category] do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.association :sub_themes, as: :select, collection: Category.all, input_html: { multiple: true } %>
<%= f.submit "Enregister" %>
<% end %>
表单已正确显示,但在创建时出现错误,并且关联未保留。
不允许的参数::sub_theme_ids
这是一个非常常见的误解,认为您需要嵌套属性来简单分配(现有记录),但事实并非如此。您只需要白名单 category[sub_theme_ids][]
:
def category_params
params.require(:category).permit(
:name,
sub_theme_ids: []
)
end
accepts_nested_attributes_for
仅当您想以相同的形式创建类别及其子主题时才需要。在这种情况下,您需要使用 fields_for
(用简单的形式包装为 simple_fields_for
):
<%= simple_form_for [:admin,@category] do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.association :sub_themes, as: :select, collection: Category.all, input_html: { multiple: true } %>
<fieldset>
<legend>Subthemes</legend>
<%= f.simple_fields_for(:sub_themes) do |st| %>
<%= st.input :name %>
<% end %>
</fieldset>
<%= f.submit "Enregister" %>
<% end %>
def category_params
params.require(:category).permit(
:name,
sub_theme_ids: [],
sub_theme_attributes: [ :name ]
)
end
我有一个 self_referenced 模型(类别):
class Category < ApplicationRecord
has_many :sub_themes_association, class_name: "SubTheme"
has_many :sub_themes, through: :sub_themes_association, source: :sub_theme
has_many :inverse_sub_themes_association, class_name: "SubTheme", foreign_key: "sub_theme_id"
has_many :inverse_sub_themes, through: :inverse_sub_themes_association, source: :category
accepts_nested_attributes_for :sub_themes
end
我想用他的 sub_themes 协会以相同的形式创建一个类别模型。
例如数字 => 类别和 SEO 和编码 => sub_themes
class Admin::CategoriesController < AdminController
def index
@categories = Category.all
end
def new
@category = Category.new
@sub_themes = @category.sub_themes.build
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to admin_categories_path
else
render :new
end
end
private
def category_params
params.require(:category).permit(
:name,
sub_theme_attributes: [
sub_theme_ids:[]
]
)
end
end
和表格:
<%= simple_form_for [:admin,@category] do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.association :sub_themes, as: :select, collection: Category.all, input_html: { multiple: true } %>
<%= f.submit "Enregister" %>
<% end %>
表单已正确显示,但在创建时出现错误,并且关联未保留。
不允许的参数::sub_theme_ids
这是一个非常常见的误解,认为您需要嵌套属性来简单分配(现有记录),但事实并非如此。您只需要白名单 category[sub_theme_ids][]
:
def category_params
params.require(:category).permit(
:name,
sub_theme_ids: []
)
end
accepts_nested_attributes_for
仅当您想以相同的形式创建类别及其子主题时才需要。在这种情况下,您需要使用 fields_for
(用简单的形式包装为 simple_fields_for
):
<%= simple_form_for [:admin,@category] do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.association :sub_themes, as: :select, collection: Category.all, input_html: { multiple: true } %>
<fieldset>
<legend>Subthemes</legend>
<%= f.simple_fields_for(:sub_themes) do |st| %>
<%= st.input :name %>
<% end %>
</fieldset>
<%= f.submit "Enregister" %>
<% end %>
def category_params
params.require(:category).permit(
:name,
sub_theme_ids: [],
sub_theme_attributes: [ :name ]
)
end