text_field用于has_many中的额外属性

时间:2015-04-21 20:20:09

标签: ruby-on-rails ruby ruby-on-rails-4 has-many-through jointable

  • ruby​​ 2.1
  • rails 4.2.1

我很难在联接表中编辑/创建额外参数的text_field。

我有3个基本型号:

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

到目前为止我允许的参数:

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

最后,产品形式:

= 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          

我可以很好地创建和保存标签,但是,如何编辑/创建&#34;项目&#34;连接表中的属性&#34;标记&#34;?

最终更新

在这里找到了类似的问题和答案:Rails has_many through form with checkboxes and extra field in the join model

0 个答案:

没有答案