Rails form_tag remote:true处理为HTML(不是* / *),没有'数据'哈希

时间:2014-11-24 15:54:30

标签: jquery ruby-on-rails ajax forms

我有工作表单,通过AJAX提交,正确处理为' / '。这是一个有额外的确认数据哈希(并需要提交确认)。 当我想要表格而没有确认时,ale看起来没问题,但是调用被视为HTML并因缺少模板而失败

一个页面都形成了他们的“东西”:

<h1>GOOD ajax call as */* looking for 'coupon_apply.js.erb'</h1>

        <%= form_tag(coupon_apply_path, method: :post, data: { confirm:  t('are_you_sure') }, id: 'apply-coupon', remote: true) do |f| %>
            <div class="clearfix">
              <label for="coupon"><%= t('coupon.coupon').capitalize %>:</label>
                <input type="text" name="coupon" id="coupon" value=""/>
              <button type="submit" value="Apply to your order" class="submit">
                <span><span><%= t('coupon.apply_order') %></span></span>
              </button>
            </div>
        <% end %>

<h1>BAD (maybe) ajax call as HTML looking for 'coupon_apply.html.erb'</h1>

        <%= form_tag(coupon_apply_path, method: :post, id: 'apply-coupon', remote: true) do |f| %>
            <div class="clearfix">
              <label for="coupon"><%= t('coupon.coupon').capitalize %>:</label>
               <input type="text" name="coupon" id="coupon" value=""/>
              <button type="submit" value="Apply to your order" class="submit">
                <span><span><%= t('coupon.apply_order') %></span></span>
              </button>
            </div>
        <% end %>

差异仅在于data: { confirm: t('are_you_sure') },的第一种形式。 两种形式都使用data-remote =“true”呈现:

 <form accept-charset="UTF-8" action="/cs/kosik/aktivovat-slevovy-kupon" data-confirm="Jste si jistí?" data-remote="true" id="apply-coupon" method="post">
 => Started POST "/cs/kosik/aktivovat-slevovy-kupon" for 127.0.0.1 at 2014-11-24 16:38:40 +0100
 => Processing by CartController#coupon_apply as */*

 <form accept-charset="UTF-8" action="/cs/kosik/aktivovat-slevovy-kupon" data-remote="true" id="apply-coupon" method="post">
=> Started POST "/cs/kosik/aktivovat-slevovy-kupon" for 127.0.0.1 at 2014-11-24 16:39:28 +0100
=> Processing by CartController#coupon_apply as HTML

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试在表单中添加格式:

 <%= form_tag(coupon_apply_path, format: :js ,method: :post, data: { confirm:  t('are_you_sure') }, id: 'apply-coupon', remote: true) do |f| %>