开发者

RoR: How to authenticate user before POST

开发者 https://www.devze.com 2023-03-01 06:19 出处:网络
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 fo开发者_如何转开发rm data.

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 fo开发者_如何转开发rm 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消