Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method 'merge' for "test":String - Rails 3.1

I have a collection_select:

<%= form_for(@feedback) do |f| %>

<div class="field">
<%= f.label :poster_id %><br />
<%= f.collection_select :feedback, :poster_id, @users, :id, @user.username, :prompt => "Select your username" %><br />
<%= f.number_field :poster_id %>
</div>
<% end %>

This is the error message:

ActionView::Template::Error (undefined method `merge' for "test":String):
    15:     
    16:   <div class="field">
    17:     <%= f.label :poster_id %><br />
    18:     <%= f.collection_select :feedback, :poster_id, @users, :id, @user.username %><br />
    19:     <%= f.number_field :poster_id %>
    20:   </div>
    21:   <div class="field">
  app/views/feedbacks/_form.html.erb:18:in `block in _app_views_feedbacks__form_html_erb__3181571289116259961_2154876620'
  app/views/feedbacks/_form.html.erb:3:in `_app_views_feedbacks__form_html_erb__3181571289116259961_2154876620'

"test" is the username that was returned by @user.username.

How do I fix that?

like image 214
marcamillion Avatar asked Dec 16 '22 09:12

marcamillion


2 Answers

the signature for collection_select is:

def collection_select method, collection, value_method, text_method, options = {}, html_options = {}

i'm not sure what you're trying to do, maybe this is what you want:

<%= f.collection_select :poster_id, @users, :id, :username, :prompt => "Select your username" %><br />
like image 160
Marian Theisen Avatar answered Jan 01 '23 09:01

Marian Theisen


I had to think of the inputs this way to get it to make sense in my head...

collection_select ‘model field’, ‘collection of options in the dropdown menu’, ‘model field for values’, ‘model field to show in the menu’

This helped me understand

like image 27
andrewcockerham Avatar answered Jan 01 '23 11:01

andrewcockerham