我有一个部分表单,它需要根据从调用者 View 传递给它的局部变量的值来呈现 remote_form_for 或 form_for。看起来……
<% 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 %>
显然,我在 <%else %> 附近遇到语法错误,因为它需要一个“结束”。
正确的做法是什么?
最佳答案
你可以做一个辅助方法
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| %>
关于ruby-on-rails - rails 条件形式/remote_form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2976297/