Pooya
Pooya

Reputation: 1012

rails keeping model validation errors

here is a frequent scenario: user has a dashboard in order to show some related information to the user. also, use can do some actions in his dashboard. according to abstraction, each of these actions, has its own controller and they are not all in the dashboard controller. for example, in order to update some information about user, you have user controller and not the dashboard controller. dashboard controller is only for rendering some information.
ok? now you have a form in your dashboard, posting the inputs to an arbitrary controller and we want to render the results(success message if succeeded and pre-populated form with errors if failed) in the dashboard. if we use redirect_to in the controller, we lose the validation errors and pre-population. if we use render, there will be uninstantiated variables in the dashboard template and we run into some errors.
what are the tricks to handle this?
P.S: there were some similiar questions but the answers were using render but it leads to the mentioned problem and doesn't work.

Upvotes: 0

Views: 42

Answers (1)

j-dexx
j-dexx

Reputation: 10406

You could do it via ajax and remove/add appropriate html elements dependent on success/failure.

Or you could have

if @user.save?
  redirect_to dashboard_path
else
  @my_instance_variable = InstanceVariable.first
  render "dashboard"
end

Upvotes: 1

Related Questions