In my Rails application, I would like to do some action after a user is signed in. For example:
I'm new to Rails, but my understanding is that I can't send a PUT request from the login controller to the other object controller, since redirects don't allow you to POST data.
I suppose I could also put some GET variables in the login path (i.e. http://example.com/login?ref=123&action=make_private) which the login controller could interpret as "make object with ID 123 private." The login controller could check for the existence of these params whenever a new login is created, and handle the updating of that object itself. But it seems like there must be a better way than to replicate the update action from the object's controller into the login controller.
One last option I've thought of is changing the workflow so that instead of seeing a "make private" button, users just see a "login to make updates" button, but it is less convenient to separate these actions than to make them possible with one click, so I would prefer to do this only as a last resort.
Am I overlooking something? What is the correct way to do something like this (if there is a correct way)?
I see what you're asking so I'll try stepping you through it.
At the point the user clicks "make private" there is an object that has already been created, correct? Assuming so then simply pass that object id along with the "make private" command when the not logged in user is redirected to the login page. This means you should have a param list with something like the following: object_id => "1", method => "make_private"
Now you have a choice, you can either update the object within the check for valid login method you should already have by doing this:
if params[:object_id] && params[:method] == "make_private"
#make object private
redirect_to objects_path(params[:object_id])
else
#normal login redirect
end
or you can redirect in your login confirm method to the object's update method and from their redirect to the object's view page with something like the following:
def login_validation
#normal login checking
if params[:object_id] && params[:method] == "make_private"
redirect_to object_path(params[:object_id])
else
#redirect to normal after login page
end
end
then in your update you'll want:
def update
#normal update stuff
redirect_to objects_path(params[:object_id])
end
Obviously you'll need something that will detect the "make private" command in your update method but that shouldn't be too difficult. If you need help on that just comment here and I'll add the instructions for that as well.
Update: If you really wanted to be fancy you could pass the redirect path itself into the login function and then just redirect to whatever the login path was provided.
For example your make private button could look like this
<%= button_to "Make Private", object_path(:id=>object_id) %>
Then it would attempt to update, realize it had no logged in user, and redirect to login page like so:
def update
if user_logged_in?
#do normal update stuff
else
redirect_to login_path(:redirect_path => object_path(params[:id])
end
Then they'd fill out their normal information and when they clicked confirm it would have an additional parameter like so:
<%= button_to "Confirm", login_validation_path(<normal login params>,params[:redirect_path]) %>
Then inside your login_validation you'd do your normal validations and then redirect to whatever path you were given.
def login_validation
#normal login stuffs
if params[:redirect_path]
redirect_to params[:redirect_path]
else
#normal redirect
end
That way your login could handle being called from multiple varying locations and correctly redirect to wherever you wanted it to.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments