Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Rails 4 strong parameters with has_many :through association?
    text
    copied!<p>I'm having trouble getting a has_many :through association working with Rails 4's strong parameters. I have a model called <code>Checkout</code> and I need to select a person from the <code>Employee</code> model in the new checkout form. Checkouts and Employees are associated through an <code>Employment</code> model.</p> <p>I'm getting this error when I try to create a new checkout:</p> <pre><code>NoMethodError in CheckoutsController#create undefined method `employee' for #&lt;Checkout:0x007ff4f8d07f88&gt; </code></pre> <p>It seems that there's something wrong with either my create action, my checkout parameters or my new checkout form. Here's the create action:</p> <pre><code> def create @user = current_user @checkout = @user.checkouts.build(checkout_params) respond_to do |format| if @checkout.save format.html { redirect_to @checkout, notice: 'Checkout was successfully created.' } else format.html { render action: 'new' } end end end </code></pre> <p>My checkout params:</p> <pre><code>def checkout_params params.require(:checkout).permit(:job, :employee_ids, :shift, :date, :hours, :sales, :tips, :owed, :collected, :notes) end </code></pre> <p>My new checkout form:</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.label :employee %&gt;&lt;br&gt; &lt;%= f.collection_select(:employee_ids, Employee.all.collect, :id, :full_name, {:prompt =&gt; "Please select"} ) %&gt; &lt;/div&gt; </code></pre> <p>But I can't figure out what has changed with Rails 4 and strong parameters. In Rails 3 this type of association and form worked for me using attr_accessible instead of strong_parameters.</p> <p><strong>Relevant Files</strong></p> <p>Full Trace of the error: <a href="https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d" rel="noreferrer">https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d</a></p> <p>models/checkout.rb: <a href="https://gist.github.com/leemcalilly/012d6eae6b207beb147a" rel="noreferrer">https://gist.github.com/leemcalilly/012d6eae6b207beb147a</a></p> <p>controllers/checkouts_controller.rb: <a href="https://gist.github.com/leemcalilly/a47466504b7783b31773" rel="noreferrer">https://gist.github.com/leemcalilly/a47466504b7783b31773</a></p> <p>views/checkouts/_form.html.erb <a href="https://gist.github.com/leemcalilly/ce0b4049b23e3d431f55" rel="noreferrer">https://gist.github.com/leemcalilly/ce0b4049b23e3d431f55</a></p> <p>models/employee.rb: <a href="https://gist.github.com/leemcalilly/46150bee3e6216fa29d1" rel="noreferrer">https://gist.github.com/leemcalilly/46150bee3e6216fa29d1</a></p> <p>controllers/employees_controller.rb: <a href="https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8" rel="noreferrer">https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8</a></p> <p>models/employment.rb: <a href="https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39" rel="noreferrer">https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39</a></p> <p>db/schema.rb: <a href="https://gist.github.com/leemcalilly/36be318c677bad75b211" rel="noreferrer">https://gist.github.com/leemcalilly/36be318c677bad75b211</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload