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
  has_and_belongs_to_many :adjectives
And my adjectives: [code=adjective.rb]class Adjective < ActiveRecord::Base
  has_and_belongs_to_many :nouns
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="<%= %>"
       value="<%= %>"
       <%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?

Re: HABTM Checkbox trick almost works...

You are in the right directly, but it is slightly different than what I have done in the past. See this post on how I have done it. If that doesn't work, or if you have any questions, feel free to ask!

Railscasts - Free Ruby on Rails Screencasts

Re: HABTM Checkbox trick almost works...

OK, I've made some minor tweaks to try to find out where the problem is. For one, I had my controller overloaded. That wasn't the problem in this case, and it's still doing the same problem, but my code sure is neater.

Anyway, this might seem really basic, but don't I need some sort of "Submit" button to tell the system what I want to do? Neither one of the above-mentioned examples I read talked about that. It seems there has to be more to it than this... and I have trouble believing that Rails would figure it out "automagically". After all, when you check the box, that should actually add a record to the adjective-noun table, and if you uncheck it, I'd like for it to delete that link between the two.

Would anyone have any thoughts on this matter? Thanks in advance...

Re: HABTM Checkbox trick almost works...

Yeah, a submit button is necessary if you are not doing AJAX. Youl would place the checkboxes in the example I gave in a form with a submit button like this:

<%= start_form_tag :action => 'create' %>
  ...checkboxes go here...
  <%= submit_tag 'Submit' %>
<%= end_form_tag %>

Railscasts - Free Ruby on Rails Screencasts