Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 has_many :through form with dropdown menus?
    primarykey
    data
    text
    <p>I'm trying to implement a has_many :through join in Rails 3 (with Formtastic) and I'm a little stuck. I have the models set up as follows:</p> <p><b>Models:</b></p> <pre><code>class Project &lt; ActiveRecord::Base has_many :employees, :through =&gt; :teams has_many :teams class Employee &lt; ActiveRecord::Base has_many :projects, :through =&gt; :teams has_many :teams class Team &lt; ActiveRecord::Base belongs_to :project belongs_to :employee </code></pre> <p>And this line gives me a multi-select box in the projects view that allows employees to be selected:</p> <p><b>View:</b></p> <pre><code>&lt;%= f.input :employees, :as =&gt; :select %&gt; </code></pre> <p>So far this gets the job done, but what I'd LIKE to have is a separate dropdown box to select each employee's name, then their role in the project. I can't figure out the form code to get me there...</p> <hr> <p><strong>EDIT:</strong></p> <p>As suggested, I've gotten the code from <a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">Railscast 197: Nested Model Forms</a> working and it's part-way there. This is what I have in the view:</p> <pre><code>&lt;%= f.semantic_fields_for :employees do |builder| %&gt; &lt;%= render 'employee_fields', :f =&gt; builder %&gt; &lt;% end %&gt; &lt;%= link_to_add_fields "add employee", f, :employees %&gt; </code></pre> <p>and the 'employee_fields' partial:</p> <pre><code>&lt;p class="fields"&gt; &lt;%= f.input :name, :as =&gt; :select, :collection =&gt; Employee.find(:all) %&gt; &lt;%= f.hidden_field :_destroy %&gt; &lt;%= link_to_remove_fields "remove", f %&gt; &lt;/p&gt; </code></pre> <p>But right now this creates a new employee rather than a new team (project-employee join record), so I think it's acting as a <code>has_many</code> rather than a <code>has_many :through</code>. How can I edit this so that the <code>:name</code> input above adds a record to <code>project[employee_ids][]</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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