6

I have a FinancialDocument#document_type model attribute. I'd like to let the user select the document type from an HTML select menu populated by an Array of Strings...

doctypes = [ 'Invoice', 'Packing slip', 'Other' ]

For each option, the displayed label and returned value would be identical.

I looked at the select and collection_select helpers, but they seem geared toward selecting a child model, not merely a String value. I couldn't discover how to bend them to my purpose.

Here's how I'm trying to do it (I'm using Haml, not Erb)...

form_for(@financial_document) do |f|
  - doctypes = [ 'Invoice', 'PS', 'Packing slip', 'Other' ]
  = f.collection_select @financial_document, :document_type, \
      doctypes, :to_s, :to_s, :include_blank => true

I get this error...

undefined method `merge' for :to_s:Symbol

Is there a different helper that I could use for this? Or a way to use select or collection_select?

2 Answers 2

11

Is doctypes an ActiveRecord collection? Looking at the code it doesn't seems so. You can use the select helper.

= f.select :document_type, doctypes, :include_blank => true

Also, you don't need to pass @financial_document if you call the tag on the form object created with form_for.

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

Comments

1
doctypes.map!{|d| [d]}
f.select(@financial_document, :document_type, doctypes)

will do it I think.

1 Comment

Thanks, but that didn't work for me... undefined method `merge' for [["Invoice"], ["PS"], ["Packing slip"], ["Other"]]:Array

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.