avalente1
avalente1

Reputation: 246

Rails form_tag unknown format error as js format with remote: true

I'm trying to add a form to a modal and then submit the form via JS format with remote: true but the form seems to be submitted as HTML instead, causing an unknown format issue. Any help would be appreciated.

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

Form:

</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 %>

Controller:

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

I've also tried adding format: :js in the form_tag but still receive the same error.

Upvotes: 3

Views: 3454

Answers (2)

mrcasals
mrcasals

Reputation: 1171

I know the question is old, but I got the same problem and found out my application.js was not requiring jquery_ujs. I added it to my application.js file:

//=require jquery
//=require jquery_ujs

Now remote links and forms work as expected :)

Upvotes: 6

murz
murz

Reputation: 115

I believe your problem may stem from a form_tag being with another form (form_tag, form_for, or html form). If this is the case, just find a way within the html for the first form to begin and end and then start the second form after the first ends.

<%= 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 %>

Upvotes: 3

Related Questions