javascript - Rails 3 406 尝试使用 format.js 时出现 Not Acceptable 错误,也无法更改内容类型

标签 javascript ruby-on-rails content-type

Possible Duplicate:
Weird “406 not acceptable” error

我已经被困在这个问题上一整天了!在我的 Rails 3 应用程序中,我有一个"new"研究页面和一个“编辑”页面。这些页面中的每一页都具有相同的部分,可以将多个出版物添加到研究中。在我的编辑页面中,通过 .js 更新页面效果很好,但在 Studies/new 中,当我创建新出版物时,我收到 406 Not Acceptable 错误。我的出版物/创建操作如下所示:

def create
  @publication = Publication.new(params[:publication])
  @publication.display_number = @publication.get_display_number(session[:study_id])
  @publication.save
  @secondary_publications = Publication.find(:all, :order => 'display_number ASC', :conditions => {:is_primary => false, :study_id => session[:study_id]})  

 respond_to do |format|
  if @publication.save
        format.js {
            render :update do |page|
                page.replace_html 'secondary_publication_table', :partial=>'publications/table' 
            end
        }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @publication.errors, :status => :unprocessable_entity }
  end
  end
  end

我尝试了各种设置内容类型的方法(但我不必在其他页面上执行此操作...),例如

render :update, :content-type => "text/javascript" do |page|

但是当我调用该操作时,我无法获得任何内容来更改 text/html 的内容类型。帮忙?

提前致谢!

编辑: 表单中提交的代码如下所示:

<%= nested_form_for @publication, :remote => true, :html=>{:id=>'secondary_pub_form'} do |f| %>
<%= f.text_field :title %>
<%= f.text_field :author, :size => 12 %>
<%= f.text_field :country, :size => 10 %>
<%= f.text_field :year, :size => 6 %>

...等等...

 <% end %>

“nested_form_for”是我正在使用的一个插件。此设置已以相同的方式在其他页面上运行(使用此插件、加载部分内容等)

最佳答案

我不清楚这里的意图是什么。您的意思是始终拒绝 .html 和 .xml 吗?也许你想要:

respond_to do |format|
  format.html { render :action => "new" }
  format.xml  { render :xml => @publication.errors, :status => :unprocessable_entity }
  format.js do
    if @publication.save
      render :update do |page|
        page.replace_html 'secondary_publication_table', :partial=>'publications/table' 
      end
    else
      # handle failing AJAX request somehow
    end
  end
end

关于javascript - Rails 3 406 尝试使用 format.js 时出现 Not Acceptable 错误,也无法更改内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4662369/

相关文章:

javascript - Windows 应用商店蓝牙

javascript - 我的 .html() 语法有什么问题?

ruby-on-rails - 尝试在 Cucumber 功能中使用 "Examples"部分时解析问题

jquery - Backbone.js + KendoUI - 使用网格进行路由、 View 和结构化

java - 在 Spring-MVC Controller 中支持多种内容类型

javascript - 在 vue.js 中显示原始图像内容类型

javascript - 调整 Google map 标记图标图像的大小

javascript - jQuery 1.4.1 中的实时更改处理程序仍然并不总是适用于 IE 中的选择元素

ruby-on-rails - Rails 6 和 Controller #render 更改

c# - 如何为 HttpClient 请求设置 Content-Type header ?