ruby-on-rails – Rails Angular:登录/注册后重定向

我正在使用Rails Devise进行登录注册,但我的前端绝大多数都使用角度路由.当有人试图在没有登录的情况下转到特定页面时,我希望在成功登录重定向它们.通常,这将是在会话中存储路径和修改after_sign_in_path_for方法的简单问题.但是,我似乎无法找到有效获取实际路径的Rails方法.我有信心我可以通过JS做到这一点,然后将其存储为cookie(或者真的是Angular),但我更喜欢将它保存在Rails中.有没有一种我想念的方法会给我一些实际的路径,包括/#/ what / whatever?

似乎以下应该有效:

request.original_url

基于此(http://api.rubyonrails.org/classes/ActionDispatch/Request.html)

但它没有返回主题标签或以下参数.

也许是因为这个人说的话(AngularJS routing without the hash ‘#’)

The # is an old browser shortcircuit which doesn’t fire the request,
which allows many js frameworks to build their own clientside
rerouting on top of that.

有没有一种干净的方法来收集Rails中的完整路径,还是我需要进入Angular / JS?

解决方法

我有同样的问题,但使用Backbone.问题是浏览器不会将URL的锚点部分发送到服务器,因此Rails无法处理此问题,只能处理前端的JS.

我发布了适用于我的解决方法.

登录页面添加此javascript代码段(最有可能在app / views / sessions / new.html.haml中)

:javascript
  // keeping the hash route into a cookie to retrieve it after sign in redirect
  cname = 'hash_route'
  cvalue = window.location.hash
  // checking if the hash value is blank,don't overwrite the cookie
  // this check covers the case of user giving wrong credentials in login
  // then successfully login within the expiration time of the cookie
  // other cases that aren't supported are relatively rare
  if(cvalue.length){ 
    var d = new Date();
    d.setTime(d.getTime() + (3*60*1000)); // enough time to log in
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
  }

在SessionsController #create中(或者你可以覆盖Devise方法“after_sign_in_path_for”)

def create
  # ------- Devise code --------
  self.resource = warden.authenticate(auth_options)
  set_flash_message(:notice,:signed_in) if is_navigational_format?
  sign_in(resource_name,resource)

  # ------- This is the code --------
  # handling the case of redirecting to a link that had a hash route    
  location = after_sign_in_path_for(resource)  

  if cookies[:hash_route].present?
     location+= cookies[:hash_route]
     cookies.delete :hash_route
  end

  respond_with resource,:location => location
end

我希望这有帮助.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...