0

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)

Community
  • 1
  • 1
Sarah
  • 516
  • 10
  • 35

4 Answers4

2

Check your application.js for require jquery_ujs

//= require jquery_ujs
Sergey
  • 738
  • 7
  • 11
2

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
zetetic
  • 47,184
  • 10
  • 111
  • 119
  • sorry I was unclear- I'm trying to load a partial in my studies/new page via javascript only. I don't want to redirect to another page. – Sarah Jan 11 '11 at 21:09
  • What's not clear is how your code expects to deal with format = 'html', since it runs `@publication.save` before checking the format. I'm guessing the request is actually `text/html`, which is unhandled, thus the 406. – zetetic Jan 11 '11 at 21:25
  • the request is text/html, but I haven't been able to change it to text/javascript successfully. I guess that is my question - how to change the request to text/javascript. When I run your code, the request goes through but redirects to "new" which I do not want to happen. I want to replace the partial only. – Sarah Jan 11 '11 at 21:33
  • Could you edit the question and show the code for the form that gets submitted? Are you including `remote => true` in `form_for`? – zetetic Jan 11 '11 at 21:49
  • OK, I see that. Passing in :remote => true ought to set the content type correctly, but it ain't working. I'd suggest looking at the generated HTML form in Firebug to see if it's baking in the right code. – zetetic Jan 11 '11 at 22:12
  • I ended up moving the form to a completely different page, which worked. Still not sure what was going on there. Thank you for the help! – Sarah Jan 13 '11 at 20:10
0

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

respond_to :html, :js
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
0

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