Reputation: 4583
On an unsuccessful save, I would like to redirect to the previous view but with the error message.
redirect_to user_path(@user_id), errors: @user.errors
but in the view, when I check for errors
I get an undefined variable errors.
I am not using the same controller new and create, so I can't have @user.errors.any in new.html.erb. I have two different controllers, one in which form is there, and another controller which will take care of create, if the create is not happening I need to redirect to the previous controller.
Upvotes: 0
Views: 260
Reputation: 4200
You have to pass the parameters inside the redirect_to helper like below,
redirect_to user_path(id: @user_id, error: @user.errors.messages)
Please check the rake routes and pass the appropriate key for id, whether it's :id, or :user_id
Upvotes: 0
Reputation: 8604
You may need to use render instead of redirect_to
.
Something like this:
# controller_1
def step_1
@user = User.new
@user.do_something
...
end
# controller_2
def step_2
if @user.save?
# redirect to another...
else
render 'controller_1/step_1`
end
end
Then on view step_1.html.erb
, you can print out errors of @user
with @user.errors
.
Upvotes: 1