# Redirecting with flash errors

**URL:** <https://www.ruby-forum.com/t/redirecting-with-flash-errors/118340>\
**Category:** Rails\
**Created:** [October 25, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/redirecting-with-flash-errors/118340 "2007-10-25T20:44:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [October 25, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/redirecting-with-flash-errors/118340/1 "2007-10-25T20:44:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [October 26, 2007, 3:35am UTC](https://www.ruby-forum.com/t/redirecting-with-flash-errors/118340/2 "2007-10-26T03:35:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [October 26, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/redirecting-with-flash-errors/118340/3 "2007-10-26T22:00:23Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
