Note that there are some explanatory texts on larger screens.

plurals
  1. PORails add nested attribute in edit view
    text
    copied!<p>I have 2 models with many to many association as follows:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :remark_users, :dependent =&gt; :destroy has_many :designated_remarks, :through =&gt; :remark_users, :source =&gt; :remark end class Remark &lt; ActiveRecord::Base has_many :remark_users, :dependent =&gt; :destroy has_many :users, :through =&gt; :remark_users accepts_nested_attributes_for :remark_users end </code></pre> <p>And the relationship:</p> <pre><code>class RemarkUser &lt; ActiveRecord::Base belongs_to :remark belongs_to :user end </code></pre> <p>In the remarks_controller, I have this:</p> <pre><code>def add_user @remark = Remark.find(params[:id]) 3.times { @remark.remark_users.build } end </code></pre> <p>To a remark is possible to associate many users. The association would have to take place after the remark is being created, since it has to pass a validation first.</p> <p>So I want to add users to a remark after it is validated. Anyway, my form, which is below, doesn't show any fields for choosing users, only the remark data is shown. </p> <pre><code>&lt;p&gt; &lt;strong&gt;Description:&lt;/strong&gt; &lt;%= @remark.description %&gt; &lt;/p&gt; &lt;h4&gt;Add Users&lt;/h4&gt; &lt;% form_for @remark do |f| %&gt; &lt;% f.fields_for :@designated_user do |u| %&gt; &lt;p&gt;User:&lt;br /&gt; &lt;%= u.collection_select(:user_id, User.all, :id, :user_name, {:include_blank =&gt; true}, {:class=&gt;'form-control'}) %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Can anyone help me? Thanks!</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