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

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

  VALID_GENDERS = ["Mand", "Kvinde"]

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

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

  START_YEAR = 1900

  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 =>
                      :message => "skal være på 4 tegn"

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

[code=]<% form_for :spec do |form| %>
  <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 class="form_row">
    <label for="birthdate">Birthdate:</label>
    <%= date_select :spec, :birthdate,
                           :start_year => Spec::START_YEAR,
                           :end_year =>,
                           :include_blank => true,
                           :order => [:day, :month, :year] %>

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

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


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

  <%= submit_tag "Update", :class => "submit" %>
<% end %>[/code]
[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 => ""


  def self.down
    drop_table :specs
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] {|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 %>

And my contoller looks like this

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