Wednesday, 15 February 2012

ruby on rails - Create one object and update another in one request -



ruby on rails - Create one object and update another in one request -

in rails application, action after user signed in. example:

a user browsing site. has not logged in. the user creates new object on site. since user not logged in, object publicly visible. the user wants create object private. clicks "make private" button. since not logged in, site not know associate private object with. redirects him login form. the user logs account. uses standard restful post request log him in. -- , part need help -- updates object create private. redirects him (now private) object view.

i'm new rails, understanding can't send set request login controller other object controller, since redirects don't allow post data.

i suppose set variables in login path (i.e. http://example.com/login?ref=123&action=make_private) login controller interpret "make object id 123 private." login controller check existence of these params whenever new login created, , handle updating of object itself. seems there must improve way replicate update action object's controller login controller.

one lastly alternative i've thought of changing workflow instead of seeing "make private" button, users see "login create updates" button, less convenient separate these actions create them possible 1 click, prefer lastly resort.

am overlooking something? right way (if there is right way)?

i see you're asking i'll seek stepping through it.

at point user clicks "make private" there object has been created, correct? assuming pass object id along "make private" command when not logged in user redirected login page. means should have param list following: object_id => "1", method => "make_private"

now have choice, can either update object within check valid login method should have 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 can redirect in login confirm method object's update method , redirect object's view page following:

def login_validation #normal login checking if params[:object_id] && params[:method] == "make_private" redirect_to object_path(params[:object_id]) else #redirect normal after login page end end

then in update you'll want:

def update #normal update stuff redirect_to objects_path(params[:object_id]) end

obviously you'll need observe "make private" command in update method shouldn't difficult. if need help on comment here , i'll add together instructions well.

update: if wanted fancy pass redirect path login function , redirect whatever login path provided.

for illustration create private button

<%= button_to "make private", object_path(:id=>object_id) %>

then effort update, realize had no logged in user, , redirect login page 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 normal info , when clicked confirm have additional parameter so:

<%= button_to "confirm", login_validation_path(<normal login params>,params[:redirect_path]) %>

then within login_validation you'd normal validations , redirect whatever path given.

def login_validation #normal login stuffs if params[:redirect_path] redirect_to params[:redirect_path] else #normal redirect end

that way login handle beingness called multiple varying locations , correctly redirect wherever wanted to.

ruby-on-rails

No comments:

Post a Comment