Wednesday, June 16, 2010

Rails - array of checkboxes

Here is a quick hacky way to make an array of check boxes for a rails form.

Warning: This version of the code hasnt been tested. It most likely needs some debug.
class ModelB  < ActiveRecord::Baseclass
  attr_accessor :name
  attr_accessor :value

class ModelA < ActiveRecord::Baseclass
   has_many :model_bs

<% form_for( @model_a,
             :url  => {:action => 'new' }, 
             :html => {:method => :post } ) do |form| %>
  <ul class="checkbox_array">
    <% ModelB.find(:all, :order => 'position asc').each do |item| %>
      <%= check_box_tag 'model_a[model_b_ids][]',,
          { :id => "model_a_model_b_ids_#{}" } %>
      <label for="<%= "model_a_model_b_ids_#{}" %>"><%= h( %></label>
    <% end %>
  <%= submit_tag t('New') %>
<% end %>

1 comment:

  1. Hi, probably our entry may be off topic but anyways, I have been surfing around your blog and it looks

    very professional. It’s obvious you know your topic and you appear fervent about it. I’m developing a

    fresh blog plus I’m struggling to make it look good, as well as offer the best quality content. I have

    learned much at your web site and also I anticipate alot more articles and will be coming back soon.Thanks you.

    ASC Coding