2011-08-11 71 views
1

我使用Rails 3.0,Ruby 1.9.2和Plataformatec simple_form gem。此代碼適用於form_for,但不適用於simple_form_for:Ruby on Rails simple_form_for all.each do

<%= simple_form_for(@provider) do |f| %> 
    <% Car.all.each do |c| %> 
    <div> 
     <%= check_box_tag :car_ids, c.id, @store.cars.include?(c), :name => 'store[car_ids][]' %> 
     $<%= c.cost %> | <%= c.description %> 
    </div> 
    <% end %> 
    <div class="actions"> 
    <%= f.submit "New" %> 
    </div> 
<% end %> 

如何使其與simple_form_for配合使用?

在此先感謝!

+0

你是什麼意思'不工作'?顯示發生了什麼。顯示錯誤。顯示我們可以討論的內容 – fl00r

+0

爲什麼您的模型名稱是複數?我的意思是'汽車' – fl00r

+0

對不起,模型不應該是複數,不好的複製。 – sizzle

回答

0

的問題是在控制器代碼。

在「新」的控制器動作,我不能簡單地執行:

@provider = Provider.new(params[:provider]) 

因爲人們通常會。

相反,我必須分別處理每個參數:

@ provider.location =參數[:供應商] [:位置] 等等

對於汽車複選框,我添加的每個car_id從一時間car_ids參數的「的has_many」汽車模型協會之一:

car_ids = params[:provider][:car_ids] 
car_ids.each do |cid| 
    @provider.cars << Car.find(cid) 
end 

然後我可以叫:

@provider.save! 

它可以正確保存(我最初的問題是它沒有保存選定的汽車)。

出於某種原因,只有在此處發佈問題後,我才能夠弄清楚這一點。有趣的是如何工作。

謝謝大家的回覆!

0

您不能像form_for一樣使用simple_form

例如ther沒有任何方法simple_form寶石。只有inuput字段可以用:as選項指定。所以,你的將被轉換爲

f.input car_ids, ..., :as => :check_box 

結帳用法,RDOC和其他有用的東西https://github.com/plataformatec/simple_form