View
  • 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

Idioms

Displaying a habtm as a set of checkboxes

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

Notes:

  • 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