RoR:如何在 POST 之前验证用户身份
我有一个用户可以填写的表单,点击“创建”按钮将发出 POST 请求,以使用表单数据在数据库中创建一个新条目。
现在,只允许登录的用户创建数据库条目,但我希望所有用户都能够填写表单。仅当单击“创建”时,我才希望我的应用程序将未登录的用户重定向到登录页面。登录后,我想继续处理 POST 请求并创建数据库条目,而用户不必再次输入所有表单数据。
处理这个问题的最佳方法是什么?将 POST 数据放入会话中?但是,成功登录后,我想在原始控制器/操作中创建数据库条目(以免重复代码),并从我的应用程序中发出 POST 请求(在成功登录后方法中)奇怪的。还有更好的想法吗?
谢谢!
I have a form that users can fill out, and hitting the "Create" button will issue a POST request to create a new entry in the database with the form data.
Now, only logged in users should be allowed to create the database entry, but I want all users to be able to fill out the form. Only when clicking "Create" do I want my application to redirect users who are not logged in to the login page. Once logged in, I want to continue processing the POST request and create the database entry, without the user having to enter all the form data again.
What's the best way of handling that? Putting the POST data in the session? But then, after successful login, I want to create the database entry in the original controller/action (so as to not duplicate code), and issuing a POST request from within my application (in the after-successful-login-method) seems strange. Any better ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提及如何进行身份验证,所以我怀疑您是手动进行的。如果您使用(我强烈建议您这样做)像 Devise 这样的 gem 进行注册,您只需将 Devise 的身份验证助手添加到您的控制器即可。
要手动执行此操作,每次用户注册时,您都需要设置一个与他/她关联的会话 ID 变量。然后,您只需检查他/她是否已注册即可。如果不是,您只是不允许该操作并重定向。
You're not mentioning how you do your authentication, so i suspect that you do it manually. If you use(and i would strongly recommend you do), a gem like Devise for registration, you just need to add Devise's authentication helper to your controllers, to do that.
To do it manually, every time the user registers, you set a session id variable that is tied to him/her. Then, you just check that to see whether he/she is registered. If not, you just do not allow the action and redirect.