Topic: Three select into one

I have three select lists in one form. They contain plain words, characteristics. I plan to get them into the same column in mysql. But it doesn't work. I think it just picks the last one of the three selects. How do I do that?

<div class="objselect">
<%= select('publicsurvey', 'wordlist', @words, :include_blank => true ) %><br />
<%= select('publicsurvey', 'wordlist', @words, :include_blank => true ) %><br />
<%= select('publicsurvey', 'wordlist', @words, :include_blank => true ) %>
</div>

Re: Three select into one

When multiple form fields have the same name they overwrite each other, which is why only the last value is being received.

One solution is to place "[]" at the end of the name attribute for the field, to do that try this:

<% 3.times do %>
  <%= select('publicsurvey', 'wordlist', @words, {:include_blank => true}, {:name => 'publicsurvey[wordlist][]'} ) %><br />
<% end %>

Now when the form is submitted the params[:publicsurvey][:wordlist] value will be an array of the selected options.

Railscasts - Free Ruby on Rails Screencasts

Re: Three select into one

ryanb wrote:

When multiple form fields have the same name they overwrite each other, which is why only the last value is being received.

One solution is to place "[]" at the end of the name attribute for the field, to do that try this:

<% 3.times do %>
  <%= select('publicsurvey', 'wordlist', @words, {:include_blank => true}, {:name => 'publicsurvey[wordlist][]'} ) %><br />
<% end %>

Now when the form is submitted the params[:publicsurvey][:wordlist] value will be an array of the selected options.

Couldn't get this working. I only get one word. Hmm. Should I do something before it's entered into the db?

Re: Three select into one

Well I got it working now, when I kept the {} surrounding {:include_blank => true}, as I didn't have before. But the result in the db is ---.

Re: Three select into one

When I print it on the resulting page I get the correct string, but all words are together without spaces. How can I add space?

Re: Three select into one

The params[:publicsurvey][:wordlist] is actually an array. You'll need to do something like this:

params[:publicsurvey][:wordlist] = params[:publicsurvey][:wordlist].join(' ')

This is kind of a hack though, it's probably better to do this in the model:

def wordlist_arr
  wordlist.split(' ') if wordlist
end

def wordlist_arr=(wordlist_arr)
  self.wordlist = wordlist_arr.join(' ')
end


You can then use wordlist_arr just like an attribute:

<% 3.times do %>
  <%= select('publicsurvey', 'wordlist_arr', @words, {:include_blank => true}, {:name => 'publicsurvey[wordlist_arr][]'} ) %><br />
<% end %>

Then it should work correctly.

Railscasts - Free Ruby on Rails Screencasts

Re: Three select into one

Thanks. I'll try that.

Or I could just name the select as _tags (I can do that with text_field, so must be possible with a select, is it 'select_tag') and then add it as a array.join(' ') in a hidden field with 'publicsurvey', 'wordlist' and send that onsubmit. How do I do onsubmit in RoR?

Re: Three select into one

The reason I didn't suggest using select_tag is (AFAIK) it doesn't have an :include_blank parameter.

I'm not sure what you mean by placing it in a hidden field during onsubmit. You would want to do the joining in the resulting controller action.

Railscasts - Free Ruby on Rails Screencasts

Re: Three select into one

ryanb wrote:

The reason I didn't suggest using select_tag is (AFAIK) it doesn't have an :include_blank parameter.

I'm not sure what you mean by placing it in a hidden field during onsubmit. You would want to do the joining in the resulting controller action.

I mean to put the values into that one hidden field, with the correct name, on submit. Maybe less hassle and simple.

Re: Three select into one

Got it working now. But the manual is really confusing. Can someone explain this...

select_tag(name, option_tags = nil, options = {})

...in contrast to...

 select_tag('word[]', options_for_select(@words))

With the latter I got it working (creating a "free" custom select). But I can't find any info about "options_for_select" in the manual where select_tag is explained.

Re: Three select into one

There are some docs for options_for_select, it's just in a different section.

The primary difference is that "select" is designed to alter a model's attribute, where "select_tag" is just a generic select you can use for anything (not tied to a model's attribute).

Railscasts - Free Ruby on Rails Screencasts

Re: Three select into one

ryanb wrote:

There are some docs for options_for_select, it's just in a different section.

The primary difference is that "select" is designed to alter a model's attribute, where "select_tag" is just a generic select you can use for anything (not tied to a model's attribute).

I see. Thanks. :-)