简体   繁体   English

Rails 3-在登录后,Devise重定向到表单

[英]Rails 3 - Devise redirects to form after sign_in

In a blog app I want every user to access a form to post a comment. 在博客应用程序中,我希望每个用户都访问一个表单以发表评论。

When the user submit the comment form he is redirected to the Devise sign_in form if is not logged in. 用户提交评论表单时,如果未登录,他将被重定向到Devise sign_in表单。

before_filter :authenticate_user!, :except => [:index, :show, :new]

How could I once the user sign_in, redirect him to the comment form and fill all fields ? 用户一旦登录,我如何将他重定向到评论表单并填写所有字段?

Thanks in advance 提前致谢

If you want to take the user to comment form after every sign in, add in ApplicationController: 如果要在每次登录后让用户评论表单,请添加ApplicationController:

#after_sign_in_path_for is called by devise
def after_sign_in_path_for(resource_or_scope)
  comment_path...
end

If you want to take user back to the page they were at before sign in, you could save the current controller+action in session and redirect back: 如果要在登录之前将用户带回到他们所在的页面,可以将当前的controller + action保存在会话中并重定向回:

session[:pre_login_controller] = params[:controller]
session[:pre_login_action] = params[:action]

And then : 接着 :

def after_sign_in_path_for(resource_or_scope)
  if session[:pre_login_controller] && session[:pre_login_action]
    "#{session[:pre_login_controller]}/#{session[:pre_login_action]}"
  else
     some default path -- root url or comment path etc
  end
end

There are multiple solutions highlighted on Stack Overflow for redirecting to previous URL after a Devise login. Devise登录后,Stack Overflow上突出显示了多种解决方案,用于重定向到先前的URL。

This is probably the most logical solution, and there is also a solution that includes CanCan (which I recommend using) 这可能是最合乎逻辑的解决方案,并且还有一个包含CanCan的解决方案(我建议使用)

Rails: Warden/Devise - How to capture the url before login/failed access Rails:Warden / Devise-如何在登录/失败访问之前捕获URL

As for the filling in forms, I think a simple solutions would be to restrict the comment box in the first place. 至于填写表格,我认为一个简单的解决方案是首先限制评论框。 Check if user is logged in (I think in Devise it's user_signed_in?) and if user_signed_in? 检查用户是否已登录(我认为在Devise中是user_signed_in?)以及user_signed_in? else "You must link_to sign in in order to comment". 否则“您必须link_to登录才能发表评论”。 If you have the comments in an action such as #new, then you could restrict the entire new action through devise. 如果您在#new之类的操作中包含注释,则可以通过设计限制整个新操作。 For that, get rid of your :new in the authentification 'except'. 为此,在认证“ except”中摆脱您的:new。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM