• render :action => 'foo' does not execute the "foo" action method, instead, it's looking-up a template.
  • methods for generating form HTML come from FormHelper
  • methods for working with text come from TextHelper


Displaying a habtm as a set of checkboxes

  <div id="field">
      <%# stand-in when no checkboxes are selected. %>
      <%= hidden_field_tag "movie[genre_ids][]" %>
      <% for genre in @genres %>
        <%= check_box_tag "movie[genre_ids][]",, @movie.genres.include?(genre), :id => %>
        <%= h -%>
      <% end %>


  • by specifying the name to be "movie[genre_ids][]" causes Rails to create an array against the "movie[genre_ids]" key… exactly what the Movie model uses to store the foreign keys. If you didn't do it this way, you'd have to do a merge of the parameters.
  • By including a hidden_field_tag; you cover the "no checkboxes selected" problem.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License