开发者

Rails 3 406 Not Acceptable error on attempting to use format.js, Can't change content-type either [duplicate]

This question already has answers here: Closed 10 years ago.
开发者_如何学编程

Possible Duplicate:

Weird “406 not acceptable” error

I've been stuck on this ALL DAY! In my rails 3 app I have a "new" page for Studies and an "edit" page. each of these pages has the same partials to add multiple Publications to a Study. In my edit page, updating the page via .js works fine, but in studies/new, when I create a new Publication I get a 406 Not Acceptable error. My Publications/create action looks like this:

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

I've tried various ways I could find of setting the content-type (yet I don't have to do this on other pages...) such as

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

but I can't get anything to change the content type from text/html when I call the action. Help?

Thanks in advance!

Edit: the code submitted in the form looks like this:

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

... etc...

 <% end %>

the "nested_form_for" is a plugin that I'm using.. this setup has worked on other pages in the same way (using this plugin, loading a partial, etc)


It's not clear to me what the intent is here. Do you mean to always reject .html and .xml? Maybe you want:

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


Check your application.js for require jquery_ujs

//= require jquery_ujs


Try adding the following to your controller; much like a before_filter (which is exactly what this ends up being)

respond_to :html, :js


When using jQuery be sure that you have included jquery_ujs.js in your layout otherwise you could also recieve 406 http errors

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜