我正在使用simple_form在我当前的项目中创建脚手架表单。我需要一些帮助来改善我的地址CRUD。在我的db:seed之后,有很多城市注册了。
我目前的地址表是:
<%= simple_form_for @address, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :street%>
<%= error_span(@address[:street]) %>
<%= f.input :zip%>
<%= error_span(@address[:zip]) %>
<%= f.label :city_id %>
<%= f.collection_select(:city_id , City.all, :id, :name, prompt: true) %>
<%= error_span(@address[:city_id]) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
address_path, :class => 'btn btn-default' %>
<% end %>
如何首先选择状态来过滤我的citties集合?
<%= f.collection_select(:city_id, City.all, :id, :name, prompt: true) %>
由于
答案 0 :(得分:0)
将此类方法添加到模型中:
def self.by_state
order('state DESC')
end
然后在表单中使用它:
<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>