Search code examples
ruby-on-railsformsruby-on-rails-4form-helpers

Rails select form helpers not playing nice with my hash


I would like to update the Transaction object's status attribute in my edit form. Status should be a drop down list where I get to select a text value correlated with an integer value that is actually PATCHed to the object.

In other words, I have a variable that looks like this:

@statuscodes = [
{ "Working" => 1 },
{ "In progress" => 2 },
{ "Cancelled: No response from borrower" => 3  },
{ "Cancelled: No response from lender" => 4 },
{ "Cancelled: Time period too long" => 5 },
...
]

Following the docs and some other SO posts, I've tried both a f.select and a f.collection_select in what I believe to be the right syntax. Obviously not, and I've printed the errors for each that follow. Any thoughts on what I'm doing wrong?

<%= f.select :transaction, :status, options_for_select(@statuscodes), {include_blank: true} %>
<!-- undefined method `merge' for #<ActiveSupport::SafeBuffer:0x007fd9da299300> -->

<%= f.collection_select :transaction, :status, @statuscodes, :last, :first, {include_blank: true} %>
<!-- undefined method `merge' for :first:Symbol -->

<%= f.select :transaction, :status, @statuscodes, {include_blank: true} %>
<!-- undefined method `merge' for #<Array:0x007fd9e1381b30> -->

Solution

  • It should be something like this:

    <%= f.collection_select :status, @statuscodes[0], :last, :first, {include_blank: true} %>
    

    Please note that the second argument should be either a hash or array of objects on which methods provided in 3rd and 4th argument can be called.