MKK
MKK

Reputation: 2753

After performing an action on a child controller in Rails, how can I control where a visitor is redirected to?

I'm modeling some conversations in my Rails project: I have Topics as a parent model, and Comments as a child of that model.

When a user visits the http://www.example.com/topics/show/2 page, my project shows them many comments related to the given topic. On that page, if a user clicks the delete button on a comment record, a request is sent to comments_controller and executes the destroy action. The request then redirects the user to the root page.

Instead of this behavior, I want them to be redirected back to the topic page with the comments. How can I get this behavior? I'm confused about this because only the ID of the comment being deleted is passed to comments_controller from the topic view page. It doesn't pass the ID of the topic that the comment was attached to, so I can't use that to direct the user back to the /topics/show/2 page.

Upvotes: 0

Views: 87

Answers (1)

Mori
Mori

Reputation: 27789

At the end of your action:

redirect_to :back

Upvotes: 2

Related Questions