Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've done a nested has_many :through and you have to basically pass the attributes to the :companies_users model and then to the :users model </p> <hr> <h1>My Code (Rails 4.0)</h1> <p><strong>Form</strong></p> <pre><code>#views/admin/posts/new &lt;%= form_for [:admin, resource], :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;table class="resource_table"&gt; &lt;thead&gt; &lt;th colspan="2"&gt;&lt;%= params[:action].capitalize %&gt; &lt;%= resource_class %&gt;&lt;/th&gt; &lt;/thead&gt; &lt;tbody class="form"&gt; &lt;% attributes.each do |attr| %&gt; &lt;tr class="&lt;%= cycle('odd', '')%&gt;"&gt; &lt;td&gt;&lt;%= resource_class.human_attribute_name(attr) %&gt;&lt;/td&gt; &lt;td&gt; &lt;% if attr == "body" %&gt; &lt;%= f.text_area attr, :rows =&gt; 60, :cols =&gt; 80, :class =&gt; "redactor" %&gt; &lt;% else %&gt; &lt;%= f.text_field attr, :value =&gt; resource.public_send(attr).to_s %&gt; &lt;% end %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;%= f.fields_for :images_posts do |images_posts| %&gt; &lt;%= images_posts.fields_for :image do |images| %&gt; &lt;tr&gt; &lt;td&gt;Image&lt;/td&gt; &lt;td&gt;&lt;%= images.file_field :image %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;tr&gt; &lt;td&gt;Caption&lt;/td&gt; &lt;td&gt;&lt;%= images_posts.text_field :caption %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;tr class="dull"&gt; &lt;td colspan="2"&gt;&lt;%= f.submit "Go" %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;% end %&gt; </code></pre> <p><strong>Models</strong></p> <pre><code>#models/image_post.rb (the join model) class ImagePost &lt; ActiveRecord::Base #Associations belongs_to :post, :class_name =&gt; 'Post' belongs_to :image, :class_name =&gt; 'Image' #Validations validates_uniqueness_of :post_id, :scope =&gt; :image_id #Nested Association (Can upload &amp; add images from form) accepts_nested_attributes_for :image, :allow_destroy =&gt; true end #models/image.rb class Image &lt; ActiveRecord::Base #Associations has_many :products, :class_name =&gt; 'Product', :through =&gt; :images_products, dependent: :destroy has_many :images_products, :class_name =&gt; 'ImageProduct' has_many :posts, :class_name =&gt; 'Post', :through =&gt; :images_posts, dependent: :destroy has_many :images_posts, :class_name =&gt; 'ImagePost' has_many :brands, :class_name =&gt; 'Brand', :through =&gt; :brands_images, dependent: :destroy has_many :brands_images, :class_name =&gt; 'BrandImages' #Image Upload Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick' has_attached_file :image, :styles =&gt; { :medium =&gt; "x300", :thumb =&gt; "x100" }, :default_url =&gt; "", :storage =&gt; :s3, :bucket =&gt; '' :s3_credentials =&gt; S3_CREDENTIALS #Validations validates_presence_of :image, :message =&gt; "No Image Present!" end #models/post.rb class Post &lt; ActiveRecord::Base #Images has_many :images, -&gt; { uniq }, :class_name =&gt; 'Image', :through =&gt; :images_posts, dependent: :destroy has_many :images_posts, :class_name =&gt; 'ImagePost' #Nested Association (Can upload &amp; add images from form) accepts_nested_attributes_for :images_posts, :allow_destroy =&gt; true end </code></pre> <p>This was a BIG help for me: <strong><a href="https://stackoverflow.com/questions/2182428/rails-nested-form-with-has-many-through-how-to-edit-attributes-of-join-model">Rails nested form with has_many :through, how to edit attributes of join model?</a></strong></p> <hr> <p><strong>Diagnosing Your Issue</strong></p> <p>I posted my code to give you a working example of what you can do (mine still needs refinement, but it works)</p> <p>Looking at your code, you should add "accepts_nested_attributes_for" into your companies_users.rb model like this:</p> <pre><code>accepts_nested_attributes_for :user </code></pre> <p>In your companies model, change your accepts_nested_attributes_for to:</p> <pre><code>accepts_nested_attributes_for :companies_users </code></pre>
 

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