2011-09-13 42 views
4

我有一個collection_select:未定義方法 '合併' 爲 「測試」:字符串 - 的Rails 3.1

<%= 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 %> 

這是錯誤消息:

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' 

「測試」 是爲用戶名由@user.username返回。

我該如何解決這個問題?

回答

11

爲collection_select的簽名是:

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

我不知道你想做什麼,也許這就是你想要什麼:

<%= f.collection_select :poster_id, @users, :id, :username, :prompt => "Select your username" %><br /> 
+0

這正是我想要的。我很難理解'collection_select'的定義。感謝您幫助我。 – marcamillion

+0

如何設置默認值。例如。說我希望它自動選擇'@ user.id'(假設它具有正確的值)。我怎麼做?我嘗試在最後添加':default => @ user.id'標誌,但那不起作用。 – marcamillion

+2

你有沒有試過:selected => @ user.id –

1

我不得不想的輸入這種方式讓它在我的腦海中變得有意義...

collection_select'model field','collection of options in the dropdown menu','model model for values','model field to show in the菜單'

這幫我明白了

相關問題