Ruby-on-rails Rails 3 返回 HTTP 406 不可接受?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3751030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:14:08  来源:igfitidea点击:

Rails 3 returning a HTTP 406 Not Acceptable?

ruby-on-railsrouting

提问by Neil Middleton

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?

现在,除了有点古怪的命名 - 重定向总是导致 406 不可接受。可能有什么问题?

回答by gertas

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_tohow to use it properly.

删除respond_to do |format|块。因为您没有指定要响应的格式,例如format.html { #your code here }. 检查response_to 的文档如何正确使用它。

回答by ryanjones

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):

我有一个类似的错误,我的控制器只响应 JSON。我需要它来响应 HTML 也让测试工作(这才有意义):

class AdsController < ApplicationController
  respond_to :json, :html

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

我在尝试执行时收到错误:assert_redirected_to ad_url(ad)

回答by sandre89

I started having this issue after a deploy in production, even tough everything was working fine in development.

在生产中部署后,我开始遇到这个问题,即使在开发中一切都很好。

After some 15 minutes of wasted time, I finally found out that I had forgotten to commit some of the view files (like index.html.erb).

经过大约 15 分钟的浪费时间,我终于发现我忘记提交一些视图文件(如 index.html.erb)

Using tail -f log/production.logon the server revealed: FATAL -- : ActionController::UnknownFormat (SomeController#index is missing a template for this request format and variant.

tail -f log/production.log在服务器上使用显示:FATAL -- : ActionController::UnknownFormat (SomeController#index is missing a template for this request format and variant.

In development the error didn't happen because, obviously, the view file was present.

在开发中,错误没有发生,因为显然存在视图文件。