使用 Restful_authentication 在单独的控制器中对用户进行身份验证
我试图让用户尽可能快地登录,因此我希望用户能够以相同的表单登录并创建记录。
是否可以通过某种方式调用会话控制器中的 create 方法,使用来自任何控制器的 Restful_authentication 插件对用户进行身份验证,并返回经过身份验证的用户?看起来这可以很容易地完成,但我就是不知道如何在 Rails 中做到这一点。
也许是这样的:
#Records Controller def create if params[:login] && params[:password] #This method would call /session/ and pass the login/password params user = authenticate_user(params[:login'], params[:password]) end @record = Record.new(params[:record]) @record.user = user if @question.save && user flash[:notice] = 'Record was successfully created.' redirect_to(@record) end end
任何关于如何做到这一点的想法将不胜感激!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经在 Rails 2.3.4 上测试了这段代码,它可以工作;用户保持登录状态。请记住,您应该尝试重构,以便身份验证代码位于单个位置,而不是将其复制到多个控制器中。
另请注意,此代码片段中的身份验证代码是会话控制器中的身份验证代码的简化版本,&所以不处理任何“记住我”功能。
I've tested this code on Rails 2.3.4 and it works; the user remains logged in. Bear in mind that you should try to refactor so that the authentication code lives in a single place, rather than having it duplicated in several controllers.
Note also that the authentication code in this snippet is a simplified version of that in the Sessions controller, & so doesn't handle any of the 'remember me' functionality.