0

我正在使用 Rails 3.2.3,我有以下表格

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

使用以下代码创建操作:

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

相应的 js.erb 文件在那里并且按预期呈现。这是代码:

$('#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();

我的 application.js 文件如下所示:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery.purr
//= require best_in_place
//= require_tree .
// Loads all Bootstrap javascripts
//= require bootstrap

在 Chrome 和 Safari 上一切正常 - 只有 HTTP 200 响应。

然而,在 Firefox 上,第一个表单提交工作正常。但是第二个给了我一个406 not accepted错误。

检查 Firebug,内容类型text/javascript在第一个请求上设置为,但在第二个请求上更改为text/html

我尝试在 format.js 之后设置内容类型(如:){ render content_type: 'text/javascript' },并在控制器上设置标题(如:) headers["Content-Type"] = "text/javascript; charset=utf-8",但无济于事。

Any good souls out there that can shed some light on this annoying problem?

4

1 回答 1

1

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

于 2012-04-19T17:11:10.980 回答