HAS_MANY通过协会和嵌套属性 [英] Has_Many Through Associations and Nested Attributes
问题描述
我试图创建让我直接加入我的表的创建和编辑值的视图。这种模式被称为雇员。我需要能够在我加盟表中创建多个行的当一个孩子雇佣多达2本书。我有一些麻烦,我怀疑这是记在我的组织...
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. I'm having some trouble and I suspect it's down to my associations...
我有3个型号。每个孩子可以有2书:
I have 3 models. Each Child can have 2 books:
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 %>
问题是,哈希没有提交如你所期望books_attributes,它只是提出图书
The problem is, the hash is not submitting books_attributes as you'd expect, it's just submitting '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?
推荐答案
试着改变books_attributes到book_attributes强paramters为hire_param。
Try changing books_attributes to book_attributes in strong paramters for hire_param.
def hire_params
params.require(:hire).permit(:child_id, book_attributes: [ :id, :book_id, :_destroy])
end
这篇关于HAS_MANY通过协会和嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!