假设视图,控制器和模型几乎相同(显然名称除外),你将如何在你的cli中得到这个params输出:
$ "project"=>{"genres_projects_attributes"=>{"genre_id"=>["1"]}
对比这个参数输出:
$"user"=>{"production_media_users_attributes"=>[{"production_medium_id"=>["1"]}]}
注意第二行中的附加括号[]。第一行有效。第二行没有。没有错误消息。
下面的源代码,但看起来与我相同: 控制器
def user_params
params.fetch(:user, {}).permit(:id, production_media_users_attributes: [:id, :user_id, {production_medium_id: []}])
end
def project_params
params.fetch(:project, {}).permit(:id, genres_projects_attributes: [:id, :project_id, {genre_id: []}])
end
模型
class User < ActiveRecord::Base
has_many :production_media, through: :production_media_users
has_many :production_media_users
accepts_nested_attributes_for :production_media_users
end
class ProductionMedium < ActiveRecord::Base
has_many :users, through: :production_media_users
has_many :production_media_users
end
class Project < ActiveRecord::Base
has_many :genres, through: :genres_projects
has_many :genres_projects
accepts_nested_attributes_for :genres_projects
end
class Genre < ActiveRecord::Base
has_many :projects, through: :genres_projects
has_many :genres_projects
end
视图
<%= select_tag 'user[production_media_users_attributes][][production_medium_id][]', options_for_select(ProductionMedium.order(:id).collect{|g| [g.name, g.id]}), { include_hidden: true, multiple: true, required: true, placeholder: 'Choose One or More', class: 'form-control menu-select2'} %>
<%= select_tag 'project[genres_projects_attributes][][genre_id][]', options_for_select(Genre.order(:id).collect{|g| [g.name, g.id]}), { include_hidden: true, multiple: true, required: true, placeholder: 'Choose One or More', class: 'form-control menu-select2'} %>
答案 0 :(得分:0)
在production_media_users_attributes上有一个额外的冒号,是吗?
params.fetch(:user, {}).permit(:id, :production_media_users_attributes: