Question

Is there a way to redirect back to a parent record when a child record is destroyed?

Example: Within the Parents show View, children can be created using nested forms. But when removing a child through 'destroy', can the user be redirect back to the SAME Parent record verses being redirected back to the standard Parent Path?

Thanks!

Was it helpful?

Solution

Use redirect_to in your destroy action:

# assuming this is a nested resourceful route like: /parents/:parent_id/children/:id
def destroy
  parent = SomeModel.find params[:parent_id]
  child = parent.children.find params[:id]
  child.destroy

  redirect_to :back # will redirect back to the referrer (page from where you came from)
end

What that does is essentially: redirect_to request.referrer which will take the user to the page they were on when they clicked delete on the child model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top