Redirecting with flash errors

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.

I’ve been away from rails for a little bit, but doesn’t the object get
an errors method? So you could just call something like
@profile.errors[:name], or something like that. I think it is the
ActiveRecord::Errors. Check it out, hope that helps.

~Jeremy

yeh there is an errors data structure, but the problem is that isnt
going to be persistent across request because as soon as you do the
redirect its going to do @profile = current_user.profile - which will
destroy the errors.

On Oct 25, 6:35 pm, Jeremy W. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs