Check box loop

I have some trouble solving a problem.

I'm trying to make a musician community, where you'll be able to find
other musicians. The users will have to choose which kind of music style
they prefer, and which instruments they play. They should be able to
choose more than one style and instrument, so i think ill use check
boxes, but theres my problem how? I've tries check_box and
check_box_tag, but i dont really know how to use them. I have values for
the check boxes in VALID_STYLES, and would like to use some kind of
loop, so i dont have to repeat code. another problem is that I can get
one checkbox to work but not 2 or more, when i select multible
checkboxes only the value of the first gets stored in the DB

Model:
[code=]class Spec < ActiveRecord::Base
  belongs_to :user

  VALID_GENDERS = ["Mand", "Kvinde"]

  VALID_AREAS = ["","København", "Storkøbenhavn", "Århus", "Aalborg",
"Odense",
                 "Nordsjælland", "Vestsjælland", "Sydsjælland",
"Midtsjælland",
                 "Lolland/Falster", "Fyn", "Nordjylland", "Østjylland",
                 "Vestjylland", "Midtjylland", "Sønderjylland",
"Bornholm",
                 "Andet" ]

  VALID_STYLES = ["Rock", "Pop", "Elektronisk", "Hip-Hop", "R&B og
Soul",
                        "Metal", "Blues", "Jazz", "Verdensmusik",
"Country",
                        "Folkemusik", "Klassisk"]

  START_YEAR = 1900
  VALID_DATES = DateTime.new(START_YEAR)..DateTime.now
  ZIP_CODE_LENGTH = 4

  validates_inclusion_of :gender,
                         :in => VALID_GENDERS,
                         :allow_nil => true,
                         :message => "skal være mand eller kvinde"

  validates_inclusion_of :birthdate,
                         :in => VALID_DATES,
                         :allow_nil => true,
                         :message => "er ugyldig"

  validates_inclusion_of :area,
                         :in => VALID_AREAS,
                         :allow_nil => true,
                         :message => "skal ikke være opdigtet"

  validates_format_of :zip_code, :with =>
/(^$|^[0-9]{#{ZIP_CODE_LENGTH}}$)/,
                      :message => "skal være på 4 tegn"

end[/code]
Controller:
[code=]def edit
    @title = "Opdater info"
    @user = User.find(session[:user_id])
    @user.spec ||= Spec.new
    @spec = @user.spec
    if param_posted?(:spec)
     if @user.spec.update_attributes(params[:spec])
      flash[:notice] = "Ændringer gemt"
     redirect_to :controller => "user", :action => "index"
    end
    end

  end[/code]
View:
[code=]<% form_for :spec do |form| %>
<fieldset>
  <legend><%= @title %></legend>

  <%= error_messages_for 'spec' %>

  <div class="form_row">
    <label for="gender">Gender:</label>
    <%= radio_button :spec, :gender, "Mand" %> mand
    <%= radio_button :spec, :gender, "Kvinde" %> kvinde
  </div>
  <div class="form_row">
    <label for="birthdate">Birthdate:</label>
    <%= date_select :spec, :birthdate,
                           :start_year => Spec::START_YEAR,
                           :end_year => Time.now.year,
                           :include_blank => true,
                           :order => [:day, :month, :year] %>
  </div>

  <div class="form_row">
    <label for="area">Area:</label>
  <%= select(:spec, :area, Spec::VALID_AREAS) %>
  </div>

  <div class="form_row">
    <label for="music_style">Musik style:</label>
<%= check_box(:spec, :music_style, {}, "Rock", nil) %> Rock

  </div>

  <%= text_field_for form, "zip_code", Spec::ZIP_CODE_LENGTH %>

  <%= submit_tag "Update", :class => "submit" %>
</fieldset>
<% end %>[/code]
Migrate:
[code=]class CreateSpecs < ActiveRecord::Migration
  def self.up
    create_table :specs do |t|

      t.column :user_id, :integer, :null => false
      t.column :gender, :string
      t.column :birthdate, :date
      t.column :area, :string, :default => ""
      t.column :zip_code, :string, :default => ""
      t.column :music_style, :mediumtext, :default => ""
     # t.column :music_instruments, :mediumtext, :default => ""
     # t.column :looking_for, :mediumtext, :default => ""

    end
  end

  def self.down
    drop_table :specs
  end
end[/code]
Please help a noob

Regards Benjamin

That's because check_box is a boolean entry (or value) control. You
would probably need to use check_box_tag.

Here, you need to loop in the view. Notice that only the checked styles
will be sent to the controller.

<% Spec::VALID_STYLES.each do |style| %>

<%= check_box_style 'valid_style[]', style %>

<% end %>

Then in the update, or create, you will have a parameter style array
with "style"=>["Pop", "Elektronisk", "Hip-Hop", "R&B og Soul"]

You can then set the style from this array by converting it to string.
ie style = params[:style]
style.select {|s| spec.music_style += s }

or something similar

hope this helps.

Serge Chevarie-Pelletier

Benjamin Jørgensen wrote:

I got the view now :slight_smile: now i just need to get the controller i'm not sure
what you mean.

What do you suggest I do? when a user saves his specs the checked
checkboxes do get stored, but when he wants to update the checkboxes
status aren't stored anymore so if he clicks update, all the info in the
checkboxes are deleted

Thanks for the answer.

my view looks like this

<div class="form_row">
    <label for="music_style">Music style:</label>
    <% Spec::VALID_STYLES.each do |music_style| %>
      <%= check_box_tag "spec[music_style][]", music_style %>
      <%= music_style %>
      <br />
   <% end %>
  </div>

And my contoller looks like this

def edit
    @title = "Opdater info"
    @user = User.find(session[:user_id])
    @user.spec ||= Spec.new
    @spec = @user.spec
    if param_posted?(:spec)
     if @user.spec.update_attributes(params[:spec])
      flash[:notice] = "Ændringer gemt"
      redirect_to :controller => "user", :action => "index"
     end
    end
  end