Closed reinvanimschoot closed 3 years ago
hi @reinvanimschoot !
can you describe what you are expecting to happen in the scenario?
i tried this workflow in a dummy app, and i am seeing a redirect back to the /books/new
page. (My app uses Books instead of Posts)
Are you expecting to see the "new" page but with no data in the form?
If so, the confusion is probably due to the fact InertiaJS keeps the component props around across the redirect. This enables a pattern like this:
def create
book = Book.new(book_params)
if book.save
redirect_to book_path(book)
else
# this will render the "new" form, but with the same props as the submitted data
redirect_to new_book_path, inertia: { errors: book.errors }
end
end
Am I correct that you expected to see the "new" form but with no data in it? Or were you expecting some other behavior?
Hi @bknoles !
Thanks for the elaborate reply!
It's actually the following:
When I am on the dashboard of my application and there is a designated button for adding a post, for example, that button would take me to posts/new
, however, on creation of said post, I would like to be able to redirect back to the dashboard, or any other place I come from. Since you can edit/create posts from different places in the app, I don't want to be limited to always having to redirect to a specific path like redirect_to posts_path
.
That is why when using redirect_back
, I assumed it would take me to the page I was on BEFORE I visited the posts/new
route, but I realise now that's a wrong assumption. I guess I would have to find another way to do this. If you have any ideas, I'd welcome them!
Regards
gotcha.. sounds like this isn't an inertia issue in that case, correct? there are probably quite a few patterns that could work, one such is in this StackOverflow question:
Are you sure it's not an Inertia issue? Since redirect_back simply doesn't work. And I was wondering if that had something to do with the routing that Inertia uses. If I press the Edit button on the dashboard and I go to the Edit page, press Save, I would assume it would take me back to the dashboard page. But for some reason this isn't working when using Inertia.
Hey @reinvanimschoot.
redirect_back
is a rails method (not an inertia specific method) that sends you back to the route that initiated the request its currently in. Inertia only decorates it to add some inertia-specific context, not to change any functionality.
Unless I'm misunderstanding, it seems like the path you're describe is:
/dashboard
route/posts/new
route./posts/create
route and hits a redirect_back
method.In this case, the last route the user was on before the request to /posts/create
(the referring route) is /posts/new
, so that's where redirect_back
sends them. Check out more here
Because this isn't inertia-specific functionality (inertia doesn't control what redirect_back
does), this doesn't seem to be an inertia issue; so I'm going to close this.
If you (or anyone else) sees different behavior in a non-inertia app with redirect_back
, please feel free to reopen!
hi @reinvanimschoot
i threw together a quick Replit without Inertia that shows the same behavior. It's just a quick scaffold of a Book
model
https://repl.it/@bknoles/OverdueFamousIntegrationtesting#app/controllers/books_controller.rb
If you start at the home page, click "create a new book", then submit the "new book" form, you'll see that Rails redirects back to the (now empty) form, and not back to the root page.
@bknoles I see now, you're right! I had the wrong expectation for that behaviour, my apologies! I got it working but using the referer so it's all good. Thanks for the hard work and the patient response!
Hi guys
I'm probably doing something wrong but whenever I have a form on a page like
/posts/new
and I pressSave
and mycreate
action in my controller usesredirect_back
nothing happens. It creates the item but it doesn't redirect back.It seems there aren't any examples of this in the documentation since it's always
redirect_to posts_path
or something similar, but I need more flexibility than that.Can anybody point me in the right direction?
Cheers!