Rails form_tag未知格式错误为js格式与remote:true

时间:2014-06-17 00:54:13

标签: javascript jquery ajax ruby-on-rails-4

我正在尝试将表单添加到模式中,然后通过JS格式使用remote: true提交表单,但表单似乎是以HTML格式提交,导致格式未知的问题。任何帮助将不胜感激。

Started POST "/create_deliv_extra" for 127.0.0.1 at 2014-06-16 20:38:17 -0400
Processing by DeliveriesController#create_deliv_extra as HTML

Completed 406 Not Acceptable in 21ms

ActionController::UnknownFormat

形式:

</br>
<%= form_tag create_deliv_extra_url, remote: true, class:"form-inline mb10 mt5", id:"extra_f_#{order.id}" do %>
  <%= text_field_tag :description, @extra.description, placeholder: "Description", "data-provide"=>"typeahead", autocomplete: :off, "data-source"=>"#{Extra.all.pluck(:description).uniq}", class:"span4" %>
  <% if order.cod == true || current_user.role == "Billing" || current_user.role == "admin" || current_user.role == "Exec" %>
    <div class="input-prepend">
      <span class="add-on">Amount $</span>
      <%= text_field_tag :amount, @extra.amount, placeholder: "$000.00", class:"input-xs" %>
    </div>
  <% end %>
  <div class="input-prepend">
    <span class="add-on">Quantity</span>
    <%= text_field_tag :quantity, @extra.quantity.present? ? "%g" % @extra.quantity : 1, class:"input-xxs" %>
  </div>
  <% next_d = order.deliveries.present? ? order.deliveries.maximum(:delivery_counter) + 1 : 1 %>
  <div class="input-prepend">
    <span class="add-on">From</span>
    <%= text_field_tag :load_start, next_d, class:"input-xxxs" %>
  </div>
  <div class="input-prepend">
    <span class="add-on">To</span>
    <%= text_field_tag :load_end, next_d, class:"input-xxxs" %>
  </div>
  <%= select_tag :extra_type, options_for_select(["Per Yard","Per Load","Flat Fee"], @extra.extra_type), class:"input-small" %>
  <%= hidden_field_tag :order_id, order.id %>
  <%= hidden_field_tag :truck_id, @id %>
  <%= button_tag "Add", class:"btn btn-danger" %>
<% end %>

控制器:

def create_deliv_extra
    @order = Order.find(params[:order_id])
    @id = params[:truck_id]
    @extra = Extra.create(amount: params[:amount], extra_type: params[:extra_type], order_id: params[:order_id], description: params[:description], quantity: params[:quantity], load_start: params[:load_start], load_end: params[:load_end])
    @extras = @order.next_deliv_extras.length > 1 ? "Extras: " + @order.next_deliv_extras : "No Extras"
    respond_to do |format|
      format.js
    end
  end

我也尝试在format: :js中添加form_tag,但仍会收到同样的错误。

3 个答案:

答案 0 :(得分:6)

我知道这个问题已经过时了,但我遇到了同样的问题,发现我的application.js并不需要jquery_ujs。我将其添加到我的application.js文件中:

//=require jquery
//=require jquery_ujs

现在remote链接和表单按预期工作:)

答案 1 :(得分:3)

我相信你的问题可能源于一个form_tag与另一种形式(form_tag,form_for或html形式)。如果是这种情况,只需在html中找到第一种形式开始和结束的方式,然后在第一次结束后开始第二种形式。

<%= form_for do %>
 ...
  <%= button_tag "#" %>
<% end %>

<%= form_tag create_deliv_extra_url, remote: true, class:"form-inline mb10 mt5", id:"extra_f_#{order.id}" do %>
  ...
  <%= button_tag "Add", class:"btn btn-danger" %>
<% end %>

答案 2 :(得分:0)

Rails似乎认为它是HTML:

Processing by DeliveriesController#create_deliv_extra as HTML

您可以尝试将format: js 添加到网址delivr_extra_path(format: :js)),并确保您当前拥有.js.erb视图。

同时检查js的http动词是否与你的路线允许的动词相同。