Radio buttons for a has_many and belongs_to association
I have a has_many and belongs_to association.
class Link < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :links
end
In the index and show I have <%= link.property.name %>
and开发者_高级运维 it will show the Property that I assigned to the link with the console just fine.
I have a problem with figuring out how to show radio buttons in the _form that assign a property to the link (a drop down would work as well).
It seems everyone who has had this question before has ether a has_many :through or a HABTM relationship and I can't seem to adapt their answers.
Since each link has only one property, you probably want radio buttons (not check boxes). This should work (in your view)
<%= form_for @link do |f| %>
<% @properties.each do |p| %>
<%= f.radio_button :property_id, p.id %>
<%= f.label :property_id, p.name %>
<% end %>
<%= f.submit %>
<% end %>
Don't forget to set @properties = Property.all
in your controller.
精彩评论