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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
96076e53fd6c6202c162476c224c640d?d=identicon&s=25 Julie Fuller (Guest)
on 2006-05-11 19: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:
__blue
__red
__purple
__circle
__triangle
__square
__free
__not-free

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!
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-11 19: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(" ")
@mymodel.save

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

Steve
96076e53fd6c6202c162476c224c640d?d=identicon&s=25 Julie Fuller (Guest)
on 2006-05-11 20: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(" ")
OR
        selected_keywords = params[:selected_keywords]
        @item.keywords = selected_keywords.join(" ")

VIEW:

        <%= #check_box_tag('item[selected_keywords][]',"wrap") %>
OR
        <input id="item_selected_keywords"
name="item[selected_keywords][]"
        type="checkbox" value="wrap" />
OR
        <%= #check_box_tag('item[selected_keywords]',"wrap") %>
OR
        <input id="item_selected_keywords"
name="item[selected_keywords]"
        type="checkbox" value="wrap" />
OR
        <input id="item[selected_keywords]"
name="item[selected_keywords]"
        type="checkbox" value="wrap" />
OR
        <%= #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.
96076e53fd6c6202c162476c224c640d?d=identicon&s=25 Julie Fuller (Guest)
on 2006-05-11 20: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:

Controller:

        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.