0

I'm using the simple form gem and I am having a hard time creating a select input and options for an object's attribute. End result should be like:

<select name="sales_invoice[flooring_company_name]" id="sales_invoice_flooring_company_name">
  <option value=""></option>
  <option value="XL Funding">XL Funding - 11111111</option>
  <option value="AFC">AFC - xxxxx</option>
  <option value="Ally">Ally -</option>
</select>

In my controller I have:

@payment_preferences = [{:name=>"XL Funding", :account_number=>"11111111"}, {:name=>"AFC", :account_number=>"xxxxx"}, {:name=>"Ally", :account_number=>""}]

In my html.erb I have tried:

<%= f.input :flooring_company_name, label: false, collection: @payment_preferences, :label_method => :name, :value_method => :name, include_blank: true %>

Which results in error: NoMethodError - undefined method `name' for "XL FUNDING":String

So i tried:

<%= f.input :flooring_company_name, label: false, collection: @payment_preferences, :label_method => :first, :value_method => :first, include_blank: true %>

Results in:

<select name="sales_invoice[flooring_company_name]" id="sales_invoice_flooring_company_name"> 
  <option value=""></option>
  <option value="[:name, "XL Funding"]">[:name, "XL Funding"]</option>
  <option value="[:name, "AFC"]">[:name, "AFC"]</option>
  <option value="[:name, "Ally"]">[:name, "Ally"]</option>
</select>
5
  • Using a Hash for this is a bit awkward. Any reason you have an objection to an Array of Arrays? Also where is this information coming from you could create PORO Object and store a list of them in a constant. If this information is stored in the Database already then there are definitely better options Commented Mar 4, 2021 at 21:49
  • The information is coming from a combination of values in a constant (FLOORING_COMPANIES = ["AFC","Ally","Car Financial"] and a model in my db which has attributes name and account_number Commented Mar 5, 2021 at 5:00
  • Why not use the model then? This would make it much easier e.g. @payment_preferences = MyModel.where(name: FLOORING_COMPANIES) then add a method to the model def display_name; "#{name} - #{account_number}"; end then simple form collection: @payment_preferences, :label_method => :display_name, :value_method => :name Commented Mar 5, 2021 at 14:01
  • The reason I don't just use the model is because my collection is a combination of existing @payment_preferences (model) and the constant values FLOORING_COMPANIES = ["AFC","Ally","Car Financial"]). So right now I am creating the hash by quering for models, selecting certain attributes using pluck and merging that with the constant values. Commented Mar 5, 2021 at 21:28
  • Please post that code there still might be easier solutions Commented Mar 5, 2021 at 21:48

1 Answer 1

1

The recommendation in simple_form gem page is to use a collection of [value:String, name:String] so I guess one solution is to change your @payment_preferences to:

@payment_preferences = [
  ["XL Funding", "XL Funding - 11111111"],
  ["AFC", "AFC - xxxxx"],
  ["Ally", "Ally - "]
];

If you really want to pass the way you are using it seems both methods you are using are lambdas, it is a bit of overengineering but you could use a lambda, check here https://stackoverflow.com/a/6334645/4507525

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

1 Comment

Similar recommendation to what engineersmnky mentioned above. I will try Array of Arrays.

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.