2015-10-19 67 views
1

我正在使用simple_form在我當前的項目中創建我的腳手架窗體。我需要一些幫助來改進我的地址CRUD。在我的db:seed之後,有很多城市註冊了。在Simple_form中按狀態過濾城市Ruby on Rails

我目前的形式地址是:

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

有什麼輸出City.all?什麼是Cidade? – neo

+0

更正後,我的表單是用葡萄牙語,現在只是改變,問這裏。 Cidade是城市,所以我的錯City.all的輸出是城市的名稱和價值作爲編號 – Gabriel

+0

得到它,所以你想按州名排序城市?是嗎? – neo

回答

0

添加此類方法模型:

def self.by_state 
    order('state DESC') 
end 

然後在你的表單中使用它:

<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>