Hey,
I have two controllers: accounts_controller and profile_controller. Whenever a user is updating information in their profile, they can do so from an accounts controller action, which gets posted to the profile controller. However, if there are errors in updating the profile, I want to be able to redirect back to the account controller with the errors in tact. I cant just render the action from the profile controller, because the account controller sets instance variables which I dont want to repeat in the profile controller.
The example is contrived so please ignore any design decisions, controller scopes and syntax errors.
AccountsController
def profile_information @profile = current_user.profile @complex_variable = User.complex_find end
profile_information.rhtml
error_messages_for(:profile) form_for (:profile, profiles_path) do |f| # form here end
ProfileController
def update if @profile.update_attributes(params[:profile]) flash[:notice] = "Successfully updated" redirect_to profile_information_path else flash[:error] = "There are errors - see below" redirect_to profile_information_path end end
In this example, error_messages_for will never print out the errors as they are lost in the redirect. I also cannot do
def update if @profile.update_attributes(params[:profile]) flash[:notice] = "Successfully updated" redirect_to profile_information_path else flash[:error] = "There are errors - see below" render :controller => 'accounts', :action => 'profile_information' end end
as this wont set the @complex_variable. (Which i dont want to have to repeat in the profile controller)
Thanks for all your help! Do let me know if I can clarify anything.
Michael
PS. A solution which seems to work is passing the errors back in the flash itself, but this seems pretty hackish - wondering if anyone has a better idea.