Topic: Replace select with multiple checkboxes

Hi,
I have a select-box on my page and I would like to replace it through multiple checkboxes. Is there a function for this? At the moment I use form_for and f.select :fieldname, choices

<% form_for :user do |f| -%>
...
    <%= f.select :gender, User.ValidGenders %>
...
<% end -%>

Re: Replace select with multiple checkboxes

A select box only allows you to select one item (unless you specify otherwise). Checkboxes allow you to select mutliple items. What kind of relationship do you have? Perhaps what you want are radio buttons?

Railscasts - Free Ruby on Rails Screencasts

Re: Replace select with multiple checkboxes

Hi!

Yes I want checkboxes, but I don't want to generate them by hand. I'm searching for a function which takes all options as arguments and generates the checkboxes for all of them.

Re: Replace select with multiple checkboxes

You probably want the check_box method:

<% for gender in User.ValidGenders %>
  <%= check_box :user, :gender, {}, gender %> # you probably need to change this depending upon how the genders are stored
<% end %>

I still think radio buttons are a better solution, as you don't want someone to check both genders, do you?

Last edited by ryanb (2006-11-26 13:35:31)

Railscasts - Free Ruby on Rails Screencasts

Re: Replace select with multiple checkboxes

Uhm, dunno. Maybe there are some hermaphrodite visiting my site.

Thx for the help!

Re: Replace select with multiple checkboxes

Heh..

There's only one column in the database for it though, right? So only one option can be selected.

Railscasts - Free Ruby on Rails Screencasts