I’m working in Ruby on Rails, I have a products and brands, and when I’m in a Brand I want to create a new product that belongs to that brand, then I want to link_to with the brand_id.
How can I do that?
<%= link_to 'New Product', new_product_path, :class => 'btn btn-primary'%>
brand model
has_many :products, :dependent => :destroy
product model
belongs_to :brand
The routes:
The link:
If you can create products outside of a brand, you might want to use a scope in your routes:
This way, you can have a separate controller to create products within a brand:
app/controllers/brand_scope/products_controller.rb, but the path (new_brand_product_path(@brand)) is still the same.In products controller,
newaction:In
views/brand_scope/products/_new.html.erb: