2015-09-04 26 views
0

試圖找到一種方法來爲兩個函數 - 新建和編輯使用一個simple_form_for。一個simple_form_for新方法和編輯方法

= simple_form_for @news_item, :url => url_for(:action => 'create', :controller => 'news_items'), :method => 'post' do |f| = f.input :title, input_html: { class: 'title-input' } = f.input :contents, input_html: { class: 'contents-input' } .actions = f.submit 'Save', class: 'btn btn-success'

目前,這種形式將創造新的形式,每次點擊提交按鈕,但我可以給這:URL => url_for(:動作=>「更新」)呢?

回答

0

試試這個:

= simple_form_for @news_item, url: (@news_item.new_record? ? news_items_index_path(@news_item) : news_items_path(@news_item)), method: (@news_item.new_record? ? :post : :patch) do |f| 

或類似的東西,你得到的圖片...

+0

太感謝你了!由於某種原因,該行無法正常工作,但是這起作用:''= simple_form_for @news_item,:url => @ news_item.new_record? ? url_for(action:'create',controller:'admin/news_items'):url_for(action:'update',controller:'admin/news_items')do | f |''' –