2010-09-20 82 views
44

我有以下的控制器代碼:Rails 3返回HTTP 406不可接受?

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 

及以下線路:

  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"} 

現在,除了略顯怪誕的命名 - 重定向總是導致406無法接受的。什麼可能是錯的?

回答

80

刪除respond_to do |format|塊。由於您未指定迴應的格式,例如format.html { #your code here }。 檢查文檔respond_to如何正確使用它。

+0

也許這個環節有信息你正在尋找:http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution – nisevi 2015-07-23 04:41:09

9

我有一個類似的錯誤,我的控制器只響應JSON。我需要它來回復也爲HTML的測試工作(這僅是有道理的):

class AdsController < ApplicationController 
    respond_to :json, :html 

我在試圖做的時候錯誤:assert_redirected_to AD_URL(廣告)

+2

謝謝,這是什麼導致了我的406錯誤。 – patrickandroid 2013-04-13 00:11:51

+0

這隻會解決問題,如果您與'respond_with @ obj'結合使用。否則,在控制器中指定'respond_to'是noop。 – maletor 2013-06-20 07:47:11

+0

@maletor,不正確。無論是否使用「respond_with」,都會生成406個錯誤。 – 2014-12-29 18:44:24