I have a "category" table that contains different kind of "product", so I create this in the category.rb:
class Category < ActiveRecord::Base
has_many :products
end
And this in product.rb:
class Product < ActiveRecord::Base
belongs_to :categories
end
I would like to know how can I get the :categories from the product in the products/new.html.erb
EDIT: Simplified code
I recommend that you use Formtastic which will do it automatically for you. If you want to do it rails without Formtastic, solution is:
Assuming you are using partial for
new.html.erb
andedit.html.erb
, the code will go into_form.html.erb
Check out these Railscasts about complex (nested) forms and formtastic:
Complex forms (part 1) (at least check this)
Formtastic (part 1)