I have a form in an account_settings controller that updates a user object through the users_controller. The error messages are not getting passed through. Here’s a look at the code.
account_settings/account.html.haml
= form_for @user, :url => { :controller => "users", :action => "update", :id => @user.id } do |f|
- if @user.errors.any?
.alert.alert-error
%h4
Please address the following errors:
%ul
- @user.errors.full_messages.each do |msg|
%li= msg
form stuff...
account_settings_controller
def account
@user = current_user
end
users_controller
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = "User was successfully updated."
end
redirect_to :back
end
The form will not update but there are no error messages getting passed back. Any thoughts?
This happens because you are returning a redirecting. The
@userobject does not persist across the redirect. You should be doing something like:Here, we redirect when the update is successful, but if not, we render the
editaction. Hence we have access to@user, and your errors would be present in@user.errors