0

I have an array:

@positions = ["external_footer", "external_top_menu", "external_side_menu"]

I want to create a select box so it produces humanized value for option and original value for option value. So i want something like that:

<option value="external_footer">External Footer</option>

Right now i am doing it like so:

= f.input :position, collection: @positions.collect{|position| { position => position.humanize}}

But it does not work.

2
  • Mind explaining how it does not work? Does it crash or display nothing or not humanize the text? Also are you using SimpleForm? Commented Nov 12, 2012 at 5:39
  • It does not give me an error, i just produces "external_footer" => External Footer as a text for select box... Commented Nov 12, 2012 at 5:43

3 Answers 3

3

I found a solution:

= f.input :position, collection: @positions.collect{|p| [ p.humanize, p ] }

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

Comments

0

Do you have a @positions in your controller? , if not, i think you should change:

positions = ["external_footer", "external_top_menu", "external_side_menu"]

to:

@positions = ["external_footer", "external_top_menu", "external_side_menu"]

And you can try this:

= f.input :position, collection: @positions.collect { |position| position.humanize }

1 Comment

Sorry i did a typo in my example code, i have "positions" variable set as global.
0

First of all, your syntax may be wrong - you have one extra pair of braces. Try = f.input :position, collection: @positions.collect{|position| position.humanize} or = f.input :position, collection: @positions.collect(&:humanize)

Assuming you're using SimpleForm, it's possible it doesn't know what method to call for the option label and value. Try passing the :label_method and :value_method explicitly, or pass humanize as a lambda, as described here.

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.