Ruby-on-rails Rails 4 simple_form collection_select
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17702426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4 simple_form collection_select
提问by markhorrocks
How do I translate the following to simple form?
如何将以下内容翻译成简单的形式?
<%= form_for(@bill) do |f| %>
<%= f.label :location_id %>
<%= f.collection_select(:location_id, @locations, :id, :location_name,
{:selected => @bill.location_id}) %>
I can't use a simple association because @locations is the result of a where query.
我不能使用简单的关联,因为 @locations 是 where 查询的结果。
回答by Viren
Try this
尝试这个
<%= simple_form_for @bill do |f| %>
<%= f.input :location,:collection => @locations,:label_method => :location_name,:value_method => :id,:label => "Location" ,:include_blank => false %>
<%= f.button :submit %>
<%end%>
Hope this help
希望这有帮助
回答by markhorrocks
Here is the final:
这是决赛:
<%= f.input :location_id, collection: @locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: @bill.location_id %>
回答by Rails Guy
In simple form, if we have an association between location and bill, than we can do like this:
以简单的形式,如果我们在 location 和 bill 之间建立关联,那么我们可以这样做:
<%= simple_form_for @bill do |f| %>
<%= f.association :location, collection: @locations, priority: @bill.location %>
<%= f.button :submit %>
<% end %>
Hope this will help.
希望这会有所帮助。
回答by Supernini
In your location model, you can also create a :
在您的位置模型中,您还可以创建一个:
def to_label
location_name
end

