Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 returning a HTTP 406 Not Acceptable?

I have the following controller code:

  def create     @admin = Admin.new(params[:admin])     respond_to do |format|       if @admin.save         redirect_to(@admin, :notice => 'Admin was successfully created.')       else         render :action => "new"       end     end   end    def update     @admin = Admin.find(params[:id])     respond_to do |format|       if @admin.update_attributes(params[:admin])         redirect_to(admin_admins_path, :notice => 'Admin was successfully updated.')       else         render :action => "edit"       end     end   end 

and the following routes:

           admin_admins GET    /admin/admins(.:format)            {:action=>"index", :controller=>"admin/admins"}            admin_admins POST   /admin/admins(.:format)            {:action=>"create", :controller=>"admin/admins"}         new_admin_admin GET    /admin/admins/new(.:format)        {:action=>"new", :controller=>"admin/admins"}        edit_admin_admin GET    /admin/admins/:id/edit(.:format)   {:action=>"edit", :controller=>"admin/admins"}             admin_admin GET    /admin/admins/:id(.:format)        {:action=>"show", :controller=>"admin/admins"}             admin_admin PUT    /admin/admins/:id(.:format)        {:action=>"update", :controller=>"admin/admins"}             admin_admin DELETE /admin/admins/:id(.:format)        {:action=>"destroy", :controller=>"admin/admins"} 

Now, aside from the slightly whacky naming - the redirects always result in a 406 Not acceptable. What could be wrong?

like image 295
Neil Middleton Avatar asked Sep 20 '10 11:09

Neil Middleton


People also ask

What does HTTP 406 Not Acceptable mean?

The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error response code indicates that the server cannot produce a response matching the list of acceptable values defined in the request's proactive content negotiation headers, and that the server is unwilling to supply a default representation.


2 Answers

Remove respond_to do |format| blocks. Because you are not specifying to what format are you responding, e.g. format.html { #your code here } . Check documentation of respond_to how to use it properly.

like image 106
gertas Avatar answered Sep 23 '22 18:09

gertas


I had a similar error, my controller was only responding to JSON. I needed it to respond to HTML also for the tests to work (which only makes sense):

class AdsController < ApplicationController   respond_to :json, :html 

I received the error when trying to do: assert_redirected_to ad_url(ad)

like image 29
ryanjones Avatar answered Sep 24 '22 18:09

ryanjones