Forum: Ruby on Rails multiple checkbox values, convert to string, 1 db field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Julie Fuller (Guest)
on 2006-05-11 21:10
Basically what I'm trying to do is create a way for users to enter
"keywords" that will then be searchable.  I.e., when a user creates a
new Item, there is also a keywords field in the database that is a
string like "purple triangle free" -- but all the user sees when they
are creating the keywords is a series of checkboxes like:

And then the controller (?) would receive those checkbox values,
concatenate them together, and stick them in the database.  Doesn't
sound too terribly difficult.  But... I can't get it.  I try using the
check_box form helper, setting the variable to "item" and the attribute
to "keywords[]" (to create an array of checkbox values) but that doesn't
work because ActiveRecord knows that the database field is called
keywords, not keywords[], and so I can't even get the "Create Item" page
to load without an method error.  I've also tried using a select box
(less preferable) and can't get that to work, either.  I suppose I could
use JavaScript to string each checkbox value together in a hidden field
onSubmit, but that seems really inelegant and awkward.

I apologize if this is a really stupid question; I'm a complete newbie
to Ruby on Rails (background in PHP)... I love it so far but it is
frustrating in situations like this where I know exactly how to do it in
PHP but am baffled in Ruby.  :-\

Any help is much appreciated!
Stephen B. (Guest)
on 2006-05-11 21:25
(Received via mailing list)
The check_box form helper will be bound to an active record field - you
probably want plain ol' checkboxes.  There is a form helper for this
too: check_box_tag.

Then you can probably do something like this in the controller method:

@mymodel = MyModel.find(params[:id])
selected_keywords = params[:selected_keywords]
@mymodel.keywords = keywords.join(" ")

This is untested code so i may be way off :0)

Julie Fuller (Guest)
on 2006-05-11 22:15
Here's what I tried (different combinations of these):

CONTROLLER (other two lines are already there):

        selected_keywords = params[:selected_keywords[]]
        @item.keywords = selected_keywords.join(" ")
        selected_keywords = params[:selected_keywords]
        @item.keywords = selected_keywords.join(" ")


        <%= #check_box_tag('item[selected_keywords][]',"wrap") %>
        <input id="item_selected_keywords"
        type="checkbox" value="wrap" />
        <%= #check_box_tag('item[selected_keywords]',"wrap") %>
        <input id="item_selected_keywords"
        type="checkbox" value="wrap" />
        <input id="item[selected_keywords]"
        type="checkbox" value="wrap" />
        <%= #check_box_tag('item',"selected_keywords",{},{:value =>
'wrap'}) %>

And none of these work.  Maybe I'm misunderstanding you?  I always get
an undefined method error for selected_keywords, if not on the page
itself, then on the processing page.  Thanks for the help.
Julie Fuller (Guest)
on 2006-05-11 22:25
Argh. I am so sorry.  It did work.

For anyone else who googles this problem in the future and finds nothing
as I did, here's the precise thing that works:


        selected_keywords = params[:selected_keywords]
        @item.keywords = selected_keywords.join(" ")

View (as many times as needed):

        <input id="item_selected_keywords" name="selected_keywords[]"
        type="checkbox" value="exterior" />

Thanks for the help!
This topic is locked and can not be replied to.