Why can't I edit associations?

44 views
Skip to first unread message

twiddle

unread,
Jun 14, 2013, 6:41:33 PM6/14/13
to rails...@googlegroups.com
I'm pulling my hair out with this. I've been googling this problem for nearly two hours. I'm trying to get the product view to have a dropdown which lets me select a tag. Instead it just lists the tags without letting me edit them. What am I doing wrong? I have:

product.rb

class Product < ActiveRecord::Base
  has_and_belongs_to_many :tags
  attr_accessible :available, :description, :name, :price, :tag_id, :id
  accepts_nested_attributes_for :tags, allow_destroy: true
end

tag.rb


class Tag < ActiveRecord::Base
  has_and_belongs_to_many :products
  attr_accessible :name, :id, :product_id
  accepts_nested_attributes_for :products, allow_destroy: true
end

schema.rb


ActiveRecord::Schema.define(:version => 20130614182226) do

  create_table "products", :force => true do |t|
    t.string   "name"
    t.string   "description"
    t.boolean  "available"
    t.integer  "price"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "products_tags", :id => false, :force => true do |t|
    t.integer "product_id"
    t.integer "tag_id"
  end

  create_table "tags", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end
end

rails_admin.rb

RailsAdmin.config do |config|
      config.model Product do
         edit do
           field :tags, :has_and_belongs_to_many_association
         end
       end
end

Govid

unread,
Jun 15, 2013, 6:48:39 AM6/15/13
to rails...@googlegroups.com
did you try this in product model ?

  attr_accessible :tags_attributes

twiddle

unread,
Jun 15, 2013, 8:59:22 AM6/15/13
to rails...@googlegroups.com
It works! Thank you so much! :-D

twiddle

unread,
Jun 15, 2013, 12:32:03 PM6/15/13
to rails...@googlegroups.com
Oh, but it only allows me to add a brand new tag. There is no way to select a pre-existing tag?

Govid

unread,
Jun 17, 2013, 1:11:23 AM6/17/13
to rails...@googlegroups.com
what if you do the same for the join table?

attr_accessible :taggings_attributes 
accepts_nested_attributes_for :taggings, allow_destroy: true
Reply all
Reply to author
Forward
0 new messages