0
  • ruby 2.1
  • rails 4.2.1

I'm stuck with editing/creating a text_field of extra param in the join table.

I have 3 basic models:

class Product < ActiveRecord::Base
  has_many :tags, through: :taggings
  has_many :taggings
  accepts_nested_attributes_for :taggings
end

class Tag < ActiveRecord::Base
  has_many :products, through: :taggings
  has_many :taggings
end

class Tagging < ActiveRecord::Base
  belongs_to :product
  belongs_to :tag, counter_cache: :products_count
end

My permitted params so far:

class Admin::ProductsController < Admin::BaseController
  def product_params
    params[:product].permit(tag_ids: [], taggings_attributes: [:id, :product_id, :tag_id, :items])
  end
end

And, finally, the product form:

= form_for [:admin, @product] do |f|
  - Tag.order(:name).all.each do |tag|
    = f.check_box :tag_ids, { :multiple => true }, tag.id, nil
    = label(:product_tag_ids, tag.id, tag.name, class: 'checkbox_label')
      = tag.name

      = f.fields_for :taggings do |t|
        = t.hidden_field :tag_id, value: tag.id
        = t.text_field :items          

I can create and save tags nicely, but, how to edit/create the "items" attribute in the join table "taggings"?

Final Update

A similar question and the answer were found here: Rails has_many through form with checkboxes and extra field in the join model

Community
  • 1
  • 1
maiconsanson
  • 243
  • 3
  • 16
  • Have you written a migration to add the attribute to to taggings? – max Apr 21 '15 at 23:52
  • @papirtiger Yes. I did. One of the problems is: I have duplicate records in this way on create. Once for tag_ids[] and once for taggings_attributes[:items]. I need to pass both parametres for the same record. – maiconsanson Apr 22 '15 at 20:33

0 Answers0