Topic: HABTM Checkbox trick almost works...
Sorry in advance about this newbie question. We gotta start somewhere. :-/
I have what I thought should be a simple many-to-many relationship between two tables: nouns and adjectives. For example, It's possible to have a 'red apple' and a 'red brick' or a 'gray brick' (but not a 'gray apple'). I'd like to enter into a noun's detail screen, then show a list of adjectives, which can be selected or deselected with a checkbox.
I set up my noun model with a has_and_belongs_to_many: [code=noun.rb]class Noun < ActiveRecord::Base
And my adjectives: [code=adjective.rb]class Adjective < ActiveRecord::Base
I also set up a table called adjectives_nouns. I have no model for this. Do I need it? I don't think so. In it are two fields: noun_id and adjective_id.
In my controller, I've defined: [code=part of administration_controller.rb] def noun_linkto_adjective
@noun = Noun.find(@params['id'])
And I've got a view: [code=noun_linkto_adjective.rhtml]<h1>Adjectives for <%= @noun.noun_name %> </h1>
<% for adjective in @adjectives %>
<li> <input type="checkbox"
id="<%= adjective.id %>"
value="<%= adjective.id %>"
<%if @noun.adjectives.include? adjective%>checked="checked"<%end%>
> <%= adjective.adjective_name %>
<% end %>
<%= link_to "Return", :action => 'modify_nouns' %>[/code]
This gives me a pretty list of all of the adjectives for a particular noun. However, if you try to check or uncheck anything, it does not do the update properly. I've tried to follow along the CheckboxHABTM and Jeffery Hicks' Many-to-Many tutorial to come up with this.
I fear that I'm making an incorrect assumption about when Rails will make the "automagic" update. I do not have an "update" definition in my administration_controller.rb. Fact is, I've probably done myself a bit of a anti-Rails disservice, because I've put a few functions in that administration_controller to work on different things. (Adding nouns and adjectives both, for example.)
Could someone please give me a gentle nudge in the right direction?