views:

29

answers:

3

The flow for my application, for non-logined users, looks like this:

1) non-logined user does a given action that requires login (arbitrary)
2) dialog appears for non-logined user and he logins without any refreshing of the page
3) collecting of additional information if it is new user
3) the given action should be executed 

How can i do so? I believe it can be done with carefully structured callbacks. Part of the tricky part is that there are a range of potential actions

+1  A: 

You've tagged ruby-on-rails, so I guess this is going to be a rails app. Are you planning on implementing your own authentication and login system or are you going to use a gem/plugin. I ask because I wouldn't have thought the range of actions would be the tricky part if the actions were all defined in the/a controller. For example with devise adding:

before_filter :authenticate_user!

in the controller would ensure that all the actions would require a login. This can be refined further using :except like

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

With that in mind we only really need to think about the ajax parts. You could perhaps achieve this using js.erb templates and using respond_to in the method, for example

def log_in
   respond_to do |f|
     # gracefully fallback!
     f.html { redirect_to new_user_session_path }
     # or do something snazy..
     f.js
   end
end

The hard part might actually be getting the ajax log in part working, but hopefully what I've said might help the thought process.

Ryan Bates has done a good screencast on jQuery with Rails but I can't post the link...

Ben Griffiths
Here is the link http://railscasts.com/episodes/136-jquery
Ben Griffiths
Thanks Ben! Good ideas, tho i am trying to do everything in javascript and minimizing the server side logic. my solution is here: http://stackoverflow.com/questions/3692288/elegantly-passing-an-click-event-through-multiple-callbacks
ming yeow
+1  A: 

Try looking at the taconite plugin for jQuery. There was a thread on this and Ruby, but it seems to have disappeared except from Google's cache.

Peter Rowell
thanks peter! This sounds great, but it is too heavy for me. I have implemented the solution this way: http://stackoverflow.com/questions/3692288/elegantly-passing-an-click-event-through-multiple-callbacks
ming yeow
+1  A: 

Store the action as a callback in a variable:

var callback = function() { alert ("You are logged in."); };

When a non-logged user attempts to execute this action, by clicking a button, or whatever, your application should pass the callback as an argument to the log-in function like this:

show_login_form(callback);

... where callback is what will be executed on a succesful login. Putting it all together:

    var callback = function() { alert ("You are logged in."); };
    if (loggedIn)
         callback();
    else
         show_login_form(callback);

Just be sure you do some kind of server side validation, as my example is simplistic and easily hacked.

SimpleCoder
Thanks! The general idea works. I have implemented it, tho the way i executed the click event seems hackish - can u take a look here?http://stackoverflow.com/questions/3692288/elegantly-passing-an-click-event-through-multiple-callbacks
ming yeow