options_from_collection_for_select不接受hash作为参数

时间:2016-02-26 13:53:20

标签: ruby-on-rails select

我的select_tag options_from_collection_for_select看起来像这样:

<%= select_tag "dash_select", options_from_collection_for_select(session[:user_hotel_list], "id", "name") %>

我的变量session [:user_hotel_list]是哈希的集合,看起来像这样:

[{"id"=>31, "name"=>"Plaze", "contact_mail"=>"plaze@gmail.com", "contact_phone"=>"+33666027414", "address"=>"JDJDJ", "postcode"=>"92200", "city"=>"JDJDJ", "nb_room"=>nil, "created_at"=>"2016-02-26T12:12:39.214Z", "updated_at"=>"2016-02-26T12:12:39.214Z", "auditor_id"=>nil, "account_id"=>48}, {"id"=>30, "name"=>"dndjd", "contact_mail"=>"djdj@gmail.com", "contact_phone"=>"+33666027414", "address"=>"k", "postcode"=>"92200", "city"=>"kk", "nb_room"=>nil, "created_at"=>"2016-02-25T22:27:47.035Z", "updated_at"=>"2016-02-25T22:27:47.035Z", "auditor_id"=>nil, "account_id"=>48}]

不幸的是,我得到undefined method名称&#39;对于#`

如何将我的对象转换为rails options_from_collection_for_select接受的结构?

1 个答案:

答案 0 :(得分:0)

您可以使用options_for_select#collect来简化此操作。

<%= select_tag "dash_select", options_for_select(session[:user_hotel_list].collect{|h| [h['name'], h['id']]}) %>

这应该可以帮到你找到你想要的东西,并希望能节省你一些时间!

回答来自How to populate a select_tag with an array of hashes?