【问题标题】:devise not working when request header contains accept with value image/*;q=0.8当请求标头包含带有值 image/*;q=0.8 的接受时,设计不起作用
【发布时间】:2020-11-13 05:48:30
【问题描述】:

我在我的 ruby​​ on rails 应用程序中使用 devise。 我正在从笔记本电脑网络浏览器调用以下端点

https://myaddress/users/sign_in

效果很好,但是当我在移动设备中点击相同的端点时,它发送的请求标头接受为Accept: image/*;q=0.8 现在服务器响应 406 不可接受。如何强制设计 gem 接受可能包含标头接受的请求,其值为 image/*;q=0.8

【问题讨论】:

  • 究竟什么是 image/;q=0.8。你为什么要发送这些标头信息?

标签: ruby-on-rails devise


【解决方案1】:

我不确定您为什么要响应请求图像的端点,但在我看来,您正在寻找respond_to。另见this answer

我猜你想回复“任何”。要支持image MIME 类型,您可以找到一个示例here(强调我的):

如果您需要使用默认不支持的 MIME 类型,您可以 可以在 config/initializers/mime_types.rb 中注册您自己的处理程序 跟随。

Mime::Type.register "image/jpg", :jpg

respond_to 还允许您为不同的 使用任何格式:

def index   @people = Person.all

 respond_to do |format|
   format.html
   format.any(:xml, :json) { render request.format.to_sym => @people }   end end

上例中,如果格式为xml,则会渲染:

render xml: @people

或者如果格式是json:

render json: @people

any 也可以不带参数使用,在这种情况下 将使用它 对于用户要求的任何格式

respond_to do |format|   format.html   format.any { redirect_to
support_path } end

格式可以有不同的变体。

【讨论】:

    猜你喜欢
    • 2014-06-02
    • 2012-05-16
    • 2014-11-03
    • 1970-01-01
    • 2018-04-03
    • 2019-04-15
    • 1970-01-01
    • 2018-07-09
    • 2016-11-14
    相关资源
    最近更新 更多