如何解决未定义的方法`to_key' collection_select错误 - RoR

时间:2016-08-21 14:24:35

标签: ruby-on-rails

我正在尝试创建一个集合选择,但我收到了这个错误:

NoMethodError in Deal#selectuser

undefined method `to_key' for #<Conversation::ActiveRecord_Relation:0x1059f4f8>

我的控制器

def selectuser
    @conversation = Conversation.involving(current_user)
end

我的路线

  resources :deal, only: [:index, :new, :create, :selectuser] do
     collection do
      get "selectuser" # generate  get "/deal/selectuser"
    end

我的观点(conversation.html.erb)

<%= form_for @conversation do |f| %>
  <%= collection_select(:conversation, :recipient_id, Conversation.all, :recipient_id, :title) %>
  <%= f.submit  "Send", class: "btn btn-primary wide" %>

1 个答案:

答案 0 :(得分:0)

您需要将collection_select方法编写为:

<%= f.collection_select :recipient_id, Conversation.all, :recipient_id, :title %>