开发者

How to redirect to a login screen THEN perform a resource action in Ruby on Rails?

In my rails 2 application, I have a photo show page. On this photo show page, there is a text field for visitors to submit comments on the photo.

If the user who is not logged in types a comment and presses submit, I want a login/register overlay (aka modal) to appear. Once they sign in or register (via our own auth or FB connect), I want the modal to disappear and the comment to be submitted.

My question is, how do I handle this wh开发者_运维百科ile keeping code DRY, CRUD, and adherent to MVC?

Some options I've considered:

  1. Pass the comment text as a hidden field in the login/register form. Save the comment in the session/new action. This is not CRUD, and also doesn't work if someone logs in via Facebook.
  2. Save the comment text as a session variable, then after login, when the page reloads, detect the session variable in the photo/show action and save the comment. This is not CRUD.
  3. Do the same as above, but detect the session variable in the view, and save the comment. This breaks MVC.
  4. Handle the login/registration completely through AJAX, detect a successful response, and then make a call to save the comment. This breaks MVC.

Keep in mind that this behavior of asking the user to log in after submitting a comment will happen all over the site on many different types of resources (not just photos), so a DRY solution is important if at all possible.


I don't understand how #4 "breaks" mvc. MVC is only talking about the server side stuff. If the request is coming in from a browser get to a url, or a script doing an XHR, it is still "mvc" in all the same ways; request comes in to the controller, controller gets the right models, passes them to the view, and sends back the result.


Wouldn't the workflow be better if you did not display the comment form to users who could not comment (because they were not logged in). Then you could at least display some text with a link that would open the login modal and allow the user to login. Once they have logged in you can redirect them back to the photo which they were viewing along with the now displayed comment form.

I'm with re5et, the workflow seems flawed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜