Has_Many Through Associations and Nested Attributes

二次信任 提交于 2020-01-03 01:53:07

问题


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...

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 %>

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?


回答1:


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


来源:https://stackoverflow.com/questions/33582309/has-many-through-associations-and-nested-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!