Rails 4 simple_form collection_select

如何将以下内容翻译成简单的表格?

   @bill.location_id}) %> 

我不能使用简单的关联,因为@locations是where查询的结果。

试试这个

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

希望这有帮助

这是决赛:

 <%= f.input :location_id, collection: @locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: @bill.location_id %> 

以简单的forms,如果我们在位置和账单之间有关联,那么我们可以这样做:

 <%= simple_form_for @bill do |f| %> <%= f.association :location, collection: @locations, priority: @bill.location %> <%= f.button :submit %> <% end %> 

希望这会有所帮助。

在您的位置模型中,您还可以创建:

 def to_label location_name end