1

Can someone explain to me why I'm getting undefined method when I add day[] (an array) to my select tag

<%= form_for [@hourable, @hour] do |f| %>
  <%= f.select 'day[]', options_for_select(days_hours) %>
<% end %>

But when I have it like this :day:

<%= form_for [@hourable, @hour] do |f| %>
  <%= f.select :day, options_for_select(days_hours) %>
<% end %>

It works fine?

Thanks

2 Answers 2

5

In f.select helper, the first argument is the property you want to assign. There is no property day[]. What you need here is :multiple => true option. So, it will look like:

f.select(:day, options_for_select(days_hours), {}, :multiple => true) 

For more information, see the docs

Sign up to request clarification or add additional context in comments.

2 Comments

Oh okay thanks! :multiple => true allows to select multiple items in one select field right? So what if I want separate the select fields, say two different select fields but same name, would I have to just change f.select to select_tag? Thanks
Yes, then you need select_tag, as rails won't do the magic for you in that case.
2

You can also use select_tag helper:

<%= select_tag "days[]", options_for_select(days_hours), {:multiple => :multiple} %>

It's only another option. Roman's answer give you what you wanted.

Hope it helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.