加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3设计 – 如何设计回应JSON

发布时间:2020-12-16 19:02:12 所属栏目:百科 来源:网络整理
导读:我的环境是带有Devise 1.1的Rails 3.0.1.我正在开发一个移动Web应用程序,主要使用 javascript,并且希望保留尽可能多的基于 JSON的通信. 有没有办法让设计用JSON响应成功/失败消息,而不是必须遵循302重定向并解析HTML? 看着使用this. ……但是没有它工作. 解
我的环境是带有Devise 1.1的Rails 3.0.1.我正在开发一个移动Web应用程序,主要使用 javascript,并且希望保留尽可能多的基于 JSON的通信.

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

看着使用this.

……但是没有它工作.

解决方法

您可以重新定义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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读