【问题标题】:Custom devise session controller error with json带有json的自定义设计会话控制器错误
【发布时间】:2015-04-22 19:40:50
【问题描述】:

我正在使用带有简单令牌身份验证的设计,以便在 restfull webService 中进行用户身份验证。但是,当我使用无效令牌进行申请时,我收到以下错误。它可以做什么?

Started GET "/users/sign_in.json" for ::1 at 2015-04-22 16:09:27 -0300
Processing by SessionsController#new as JSON
Completed 406 Not Acceptable in 4ms

ActionController::UnknownFormat (ActionController::UnknownFormat):
  responders (2.1.0) lib/action_controller/respond_with.rb:205:in `respond_with'
  devise (3.4.1) app/controllers/devise/sessions_controller.rb:11:in `new'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'

【问题讨论】:

  • 使用有效的令牌...您能否更具体地说明您的错误是什么以及您想要做什么?
  • 我在使用无效令牌时收到此错误。但我期待 json 中的错误消息,如 {“status”:401,“message”:“无效凭据”}。新方法出现这个错误,找不到原因。

标签: ruby-on-rails json devise


【解决方案1】:

我解决了我的问题

class SessionsController < Devise::SessionsController
  respond_to :html, :json

  def new
    self.resource = resource_class.new(sign_in_params)
    clean_up_passwords(resource)
    yield resource if block_given?
    respond_with(resource, serialize_options(resource)) do |format|
      format.json { render :json => { :message => "Forbidden"},:status => :unauthorized }
    end
  end
  ...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多