[英]Has_Many Through Associations and Nested Attributes
I'm trying to create a view allowing me to create and edit values of my joining table directly. 我正在尝试创建一个视图,允许我直接创建和编辑联接表的值。 This model is called 'hires'.
该模型称为“员工”。 I need to be able to create multiple rows in my joining table for when a child hires up to 2 books.
当一个孩子租用最多2本书时,我需要能够在联接表中创建多行。 I'm having some trouble and I suspect it's down to my associations...
我遇到了一些麻烦,我怀疑这取决于我的协会...
I have 3 models. 我有3个模型。 Each Child can have 2 books:
每个孩子可以拥有2本书:
class Child < ActiveRecord::Base
has_many :hires
has_many :books, through: :hires
end
class Hire < ActiveRecord::Base
belongs_to :book
belongs_to :child
accepts_nested_attributes_for :book
accepts_nested_attributes_for :child
end
class Book < ActiveRecord::Base
has_many :hires
has_many :children, through: :hires
belongs_to :genres
end
The controller looks like this: 控制器如下所示:
class HiresController < ApplicationController
...
def new
@hire = Hire.new
2.times do
@hire.build_book
end
end
def create
@hire = Hire.new(hire_params)
respond_to do |format|
if @hire.save
format.html { redirect_to @hire, notice: 'Hire was successfully created.' }
format.json { render :show, status: :created, location: @hire }
else
format.html { render :new }
format.json { render json: @hire.errors, status: :unprocessable_entity }
end
end
end
...
private
# Use callbacks to share common setup or constraints between actions.
def set_hire
@hire = Hire.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def hire_params
params.require(:hire).permit(:child_id, book_attributes: [ :id, :book_id, :_destroy])
end
end
The view likes this: 该视图如下所示:
<%= form_for(@hire) do |f| %>
<%= f.label :child_id %><br>
<%= f.select(:child_id, Child.all.collect {|a| [a.nickname, a.id]}) -%>
<%= f.fields_for :books do |books_form| %>
<%= books_form.label :book_id %><br>
<%= books_form.select(:book_id, Book.all.collect {|a| [a.Title, a.id]}) %>
<%# books_form.text_field :book_id #%>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The problem is, the hash is not submitting books_attributes as you'd expect, it's just submitting 'books': 问题是,哈希没有按您期望的那样提交books_attributes,而只是提交了“ books”:
Processing by HiresController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xx", "hire"=>{"child_id"=>"1", "books"=>{"book_id"=>"1"}}, "commit"=>"Create Hire"}
Unpermitted parameter: books
I suspect this is because my associations for the Hire model are: 我怀疑这是因为我与雇用模型的关联是:
belongs_to :book
accepts_nested_attributes_for :book
which means I can't build the attributes correctly but I'm not sure how to solve this. 这意味着我无法正确构建属性,但不确定如何解决此问题。 Any help would be great, am I solving this problem badly?
任何帮助都会很大,我是否严重解决了这个问题?
Try changing books_attributes to book_attributes in strong paramters for hire_param. 尝试将hir_param的强参数中的books_attributes更改为book_attributes。
def hire_params
params.require(:hire).permit(:child_id, book_attributes: [ :id, :book_id, :_destroy])
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.