I’m using Rails 3.2.3 and I have the following form
<%= form_for(@item, url: list_path, remote: true, html: {id: "item-create-form", class: "form-horizontal"}) do |f| %>
<div class="input-append">
<%= f.text_field :description, id: "item-description-input", autofocus: true, placeholder: "Type and press enter." %>
</div>
<% end %>
The create action with the following code:
def create
@user = User.find_by_username(params[:username])
@item = current_user.items.build(params[:item])
respond_to do |format|
if @item.save
@item.update_attribute(:vpos, 0)
@items = Item.find_all_by_user_id(@user)
@items.each do |item|
item.update_attribute(:vpos, item.vpos + 1)
end
format.js
else
format.js {render "errors" }
end
end
end
The corresponding js.erb file is there and rendering as expected. Here’s the code:
$('#position-zero').after('<%= escape_javascript(render(:partial => @item)) %>');
$('#item-create-form').html('<%= escape_javascript(render(:partial => "item_create_form", :locals => { :@item => Item.new })) %>');
$("#item-description-input").focus();
$(".best_in_place").best_in_place();
My application.js file looks like this:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery.purr
//= require best_in_place
//= require_tree .
// Loads all Bootstrap javascripts
//= require bootstrap
Everything works great on Chrome and Safari – only HTTP 200 responses.
On Firefox, however, the first form submission works ok. But the second one gives me a 406 not accepted error.
Checking on Firebug, the content-type is set to text/javascript on the first request, but changes to text/html on the second one.
I’ve tried setting the content type after format.js (like so: { render content_type: 'text/javascript' }) and also setting headers on the controller (like so: headers["Content-Type"] = "text/javascript; charset=utf-8"), to no avail.
Any good souls out there that can shed some light on this annoying problem?
notice the second line in ur js.erb? you are adding a form inside the form. so basically when u submit the form the second time, the first form is sent as the ajax request, however, the form above it is not, giving you the problem. you should remove that second line and things should be ok