ruby-on-rails - rails 条件形式/remote_form

标签 ruby-on-rails

我有一个部分表单,它需要根据从调用者 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/

相关文章:

ruby-on-rails - 使用 HMABTM 验证所有权的最佳方式

ruby-on-rails - SQLite3::BusyException

ruby-on-rails - 向多个收件人发送电子邮件 actionmailer

ruby-on-rails - Ruby on Rails - 登录

html - 居中引导 3 导航栏

ruby-on-rails - 如何在 Rails 之外使用 Rails DateHelper 方法 time_ago_in_words?

ruby-on-rails - 将 God 进程记录到 STDOUT 而不是文件

css - 配置 tinymce-rails 以默认使用不同的字体系列

ruby-on-rails - 使 rails 3 jquery 自动完成整数列工作

ruby-on-rails - :remote => true not working when a file uploader is used in form