Julie Fuller
2006-May-11 17:10 UTC
[Rails] multiple checkbox values, convert to string, 1 db field
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! -- Posted via http://www.ruby-forum.com/.
Stephen Bartholomew
2006-May-11 17:23 UTC
[Rails] multiple checkbox values, convert to string, 1 db field
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
Julie Fuller wrote:> 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!
>
Julie Fuller
2006-May-11 18:16 UTC
[Rails] Re: multiple checkbox values, convert to string, 1 db field
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.
--
Posted via http://www.ruby-forum.com/.
Julie Fuller
2006-May-11 18:29 UTC
[Rails] Re: multiple checkbox values, convert to string, 1 db field
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!
--
Posted via http://www.ruby-forum.com/.