Rubytastic
Rubytastic

Reputation: 15501

how to correctly store values of selected checkboxes in rails?

After searching long time Im unable to find any information on how to best store values of selected checkboxes in rails.

I do something like:

  = form_for @profile, :url => request.fullpath, :method => :get,  :html => { :class => 'form-  horizontal' } do |f|
      - @contacts.each do |contact|
        = check_box_tag "contact[#{ contact.slug }]", contact.id, true
        //= label_tag "contact[#{contact.id}]", contact.slug
     = f.submit t('forms.go')

On form submit this gives me params:

Parameters: {"utf8"=>"✓", "contact"=>{"1"=>"bladadie", "2"=>"jolanteds03",
    "3"=>"jannie6674", "4"=>"henriette1305",
    "5"=>"amy6456", "6"=>"jacquelyn9001", "7"=>"florencio4872",
    "8"=>"angel410", "9"=>"rita558", "10"=>"chadd2684", "11"=>"ilene8219",
    "12"=>"sonny664", "13"=>"monique7912", "14"=>"merritt28103744",
    "15"=>"sunny80771258", "16"=>"lavinia31942066", "17"=>"jada29655747",
    "18"=>"johan16227289", "19"=>"cristobal9746087",
    "20"=>"meggie3447530"}, "commit"=>"Go »"}

How Would I correctly store the values of those checked, checkboxes?

I'm unable to find any info on this. Hope someone can shed some light!

Upvotes: 0

Views: 339

Answers (1)

felipeclopes
felipeclopes

Reputation: 4070

In your exemple you can iterate through the params:

params.each do |key, value|
   contact.create(:id => key, :slug => value
end

The create will save them automatically!

Upvotes: 2

Related Questions