2010-06-04 24 views
3

我有一個表單partial,需要根據從調用者視圖傳入的局部變量的值來呈現remote_form_for或form_for。它看起來像......rails條件表/ remote_form

<% if ajax %> 
    <% remote_form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %> 
    <% else %> 
    <% form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %> 
    <% end %> 

很顯然,我越來越近<%其他%>語法錯誤,因爲它期望的「終結」。

什麼是正確的方法來做到這一點?

回答

2

你可以做一個輔助方法

def form_or_remote_form_for object, *opts, &proc 
    if ajax 
    remote_form_for object, *opts, &proc 
    else 
    form_for object, *opts, &proc 
    end 
end 

,然後在你的看法它會僅僅是

<% form_or_remote_form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %> 
+0

這是如何爲Rails 3? – Tony 2012-01-26 13:14:47