【问题标题】:Rails 3 + devise - How to get devise to respond with JSONRails 3 + 设计 - 如何让设计以 JSON 响应
【发布时间】:2011-05-30 03:42:40
【问题描述】:

我的环境是带有 Devise 1.1 的 Rails 3.0.1。我正在开发一个移动 Web 应用程序,主要使用 javascript,并希望尽可能多地保持基于 JSON 的通信。

设计有没有办法用 JSON 响应成功/失败消息,而不必遵循 302 重定向并解析 HTML?

查看使用this

...但不让它工作。

【问题讨论】:

  • 您检查您的请求是否接受 json?

标签: ruby-on-rails json devise


【解决方案1】:

您可以重新定义 sign_in_and_redirect 和 sign_out_and_redirect 设计助手,以呈现 json 而不是重定向用户。

我无法在初始化程序中重新定义它们,所以我找到的最接近的解决方案是将其添加到 application_controller.rb :

private

# Override the default devise signin/signout process
def sign_in_and_redirect(resource_or_scope, resource=nil)
  scope      = Devise::Mapping.find_scope!(resource_or_scope)
  resource ||= resource_or_scope
  sign_in(scope, resource) unless warden.user(scope) == resource
  # redirect_to stored_location_for(scope) || after_sign_in_path_for(resource)
  render :json => {:status => :signed_in}
end


def sign_out_and_redirect(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  if Devise.sign_out_all_scopes
    sign_out_all_scopes
  else
    sign_out(scope)
  end
  #redirect_to after_sign_out_path_for(scope)
  render :json => {:status => :signed_out}
end

如果有人有更清洁的解决方案,我也很感兴趣

【讨论】:

  • 覆盖Devise::SessionsController 似乎是更清洁的解决方案,就像here 中提到的那样
猜你喜欢
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多