Create one object and update another in one request

user108338

In my Rails application, I would like to do some action after a user is signed in. For example:

  • A user is browsing the site. He has not logged in.
  • The user creates a new object on the site. Since the user is not logged in, the object is publicly visible.
  • The user now wants to make the object private. He clicks the "make private" button.
  • Since he is not logged in, the site does not know who to associate the private object with. It redirects him to the login form.
  • The user logs into his account. This uses a standard RESTful POST request to log him in. Then -- and this is the part I need help with -- it also updates the object to make it private. It then redirects him back to the (now private) object view.

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)?

MCBama

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bulk update/create in one request

From Dev

Create sheet and update data with one request

From Dev

Creating one generic object and using it to create another generic object in java

From Dev

Use response object from one request in body of another [postman]

From Dev

How is REQUEST object for a user in Django Views diffrentiated from one to another?

From Dev

How to create trigger when one rows is updated to update another table

From Dev

Add object to DB in one fragment and update listView in another

From Dev

Cast one object to another

From Dev

Cast one object to another

From Dev

Instantiating an object in another one

From Dev

How to create one database object in another database's stored procedure?

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

Best way to create a dateTime object 30 minutes after another one?

From Dev

create dynamic line from one object to another Three.js

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

Best way to create a dateTime object 30 minutes after another one?

From Dev

Create an object if one is not found

From Dev

Create a function from another one

From Dev

How to cast one object into another

From Dev

Converting object of a class to of another one

From Dev

JS copy one object to another

From Dev

Complex object inside another one

From Dev

SQLAlchemy create object in one line?

From Dev

How to create object of one Model after creating another Model's object using serialization and modelViewSet

From Dev

Same object in one case, different object in another?

From Dev

add one object to another object in javascript

From Dev

PHP how to pass `http request` object from one PHP file to another PHP file

From Dev

PHP how to pass `http request` object from one PHP file to another PHP file

From Dev

Redirecting request from one folder to another in htaccess

Related Related

  1. 1

    Bulk update/create in one request

  2. 2

    Create sheet and update data with one request

  3. 3

    Creating one generic object and using it to create another generic object in java

  4. 4

    Use response object from one request in body of another [postman]

  5. 5

    How is REQUEST object for a user in Django Views diffrentiated from one to another?

  6. 6

    How to create trigger when one rows is updated to update another table

  7. 7

    Add object to DB in one fragment and update listView in another

  8. 8

    Cast one object to another

  9. 9

    Cast one object to another

  10. 10

    Instantiating an object in another one

  11. 11

    How to create one database object in another database's stored procedure?

  12. 12

    How to create a class that one of its object is in the type of another class in VB?

  13. 13

    Best way to create a dateTime object 30 minutes after another one?

  14. 14

    create dynamic line from one object to another Three.js

  15. 15

    How to create a class that one of its object is in the type of another class in VB?

  16. 16

    Best way to create a dateTime object 30 minutes after another one?

  17. 17

    Create an object if one is not found

  18. 18

    Create a function from another one

  19. 19

    How to cast one object into another

  20. 20

    Converting object of a class to of another one

  21. 21

    JS copy one object to another

  22. 22

    Complex object inside another one

  23. 23

    SQLAlchemy create object in one line?

  24. 24

    How to create object of one Model after creating another Model's object using serialization and modelViewSet

  25. 25

    Same object in one case, different object in another?

  26. 26

    add one object to another object in javascript

  27. 27

    PHP how to pass `http request` object from one PHP file to another PHP file

  28. 28

    PHP how to pass `http request` object from one PHP file to another PHP file

  29. 29

    Redirecting request from one folder to another in htaccess

HotTag

Archive