2011-09-13 5 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 'モデルフィールド'、 'ドロップダウンメニューのオプションのコレクション'、 '値のモデルフィールド'、 'モデルフィールドメニュー '

これはわかりました

関連する問題