# Ajax request render from RJS and handling Flash notices

**URL:** <https://www.ruby-forum.com/t/ajax-request-render-from-rjs-and-handling-flash-notices/173095>\
**Category:** Rails\
**Created:** [August 2, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/ajax-request-render-from-rjs-and-handling-flash-notices/173095 "2009-08-02T15:36:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 2, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/ajax-request-render-from-rjs-and-handling-flash-notices/173095/1 "2009-08-02T15:36:41Z")

</div>

I created an rjs file to handle an ajax request to update my index view  
for a particular view template I’m working with. I’m using…

In the index.js.rjs file:

page.replace\_html(“headstart” , :partial =\> “headstart” , :object =\>  
@teamnames)  
page.replace\_html(“teamheaders” , :partial =\> “teamheaders” , :object =\>  
@teamnames)  
page.replace\_html(“team0” , :partial =\> “team0” , :object =\> @teamnames)  
page.replace\_html(“boxscore” , :partial =\> “boxscore” , :object =\>  
@teamnames)

In the index.html file the part that corresponds to this is:

\<% form\_remote\_tag :url =\> { :action =\> :index, :id =\> @teamnames } do  
%\>  
  
\<%= collection\_select(:team, :id, @teamnames, :id, :name,  
options ={ :prompt =\> “- Select Team One” }, { :name =\> ‘teamone’ } )  
%\>  
   
  
\<%= collection\_select(:team, :id, @teamnames, :id, :name,  
options ={ :prompt =\> “- Select Team Two” }, { :name =\> ‘teamtwo’ } )  
%\>  
   
  
\<%= image\_submit\_tag(’/images/matchup.jpg’, :title =\>  
“Matchup”, :onmouseover =\> “this.src=’/images/matchup\_o.jpg’”,  
:onmouseout =\> “this.src=’/images/matchup.jpg’”) %\>  
   
\<% end %\>

And finally in the controller, the issue I’m having is with regards to:

def index  
@teamnames = Team.find(:all, :order =\> “name ASC”)  
validate\_team\_select(params[:teamone], params[:teamtwo])  
end

def validate\_team\_select(teamone,teamtwo)

if teamone == teamtwo && teamone != “”  
flash[:notice] = ‘You cannot compare a team to itself!’  
redirect\_to virtual\_matchups\_path and return  
elsif teamone == “” || teamtwo == “”  
flash[:notice] = ‘You must select two unique teams!’  
redirect\_to virtual\_matchups\_path and return  
else

… My Code Routines (doesn’t apply to the problem here) …

respond\_to do |format|  
format.html  
# the format js applies to the RJS template  
format.js  
format.xml { render :xml =\> @virtuals }  
end

end

In my application layout file I have:

\<% if flash[:notice] -%\>

\<%= flash[:notice] %\>
\<%  
end -%\>

=======================

So, here’s my issue. Before I placed the ajax request handling in, if  
you selected no teams from the dropdown form, or had the same teams, or  
had one team and no other teams, the validation above would work fine.  
It’s just a custom validation (no db in virtuals so can’t do validation  
there) that checks to make sure everything is fine.

When the validation failed before, my flash notice which is in my layout  
file would spring to action. Now, the ajax request just doesn’t show  
anything. When I hit the validation errors, it just doesn’t do or  
respond to anything (no errors are shown). If I go to another page on  
my site, then the flash notification takes place.

Again, this worked fine before ajax so my guess here is that I need to  
place some type of update for the flash notice in rjs but I’m not sure  
how. In my example above, I was forcing a render back to virtuals path  
but ajax takes over that forced render now.

Any advice on how to get this working?

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 2, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/ajax-request-render-from-rjs-and-handling-flash-notices/173095/2 "2009-08-02T18:36:47Z")

</div>

I figured out how to get it done. My situation was a bit different than  
the norm but the following works:

in my rjs:

page.replace\_html(“headstart” , :partial =\> “headstart” , :object =\>  
@teamnames) if @flashsetting == false  
page.replace\_html(“teamheaders” , :partial =\> “teamheaders” , :object =\>  
@teamnames)  
page.replace\_html(“team0” , :partial =\> “team0” , :object =\> @teamnames)  
page.replace\_html(“boxscore” , :partial =\> “boxscore” , :object =\>  
@teamnames)  
page.replace\_html :notice, “”  
flash.discard

in my controller:

def validate\_team\_select(teamone,teamtwo)  
if teamone == teamtwo && teamone != “”  
flash[:notice] = ‘You cannot compare a team to itself!’  
virtual\_redirect  
elsif teamone == “” || teamtwo == “”  
flash[:notice] = ‘You must select two unique teams!’  
virtual\_redirect  
else  
virtual\_response(teamone, teamtwo)  
end  
end

def virtual\_redirect  
respond\_to do |format|  
format.html { redirect\_to virtual\_matchups\_path }  
format.js { render :update do |page| page.replace\_html :notice,  
flash[:notice] end }  
end  
end

Then I placed a notice div inside the actual view and not in the layout,  
nesting a partial to the notice alert with css coloring…

This works well in that it performs all my ajax requests through a  
standard process but if it reaches a validation error, it goes to the  
virtual\_redirect which displays the correct messages without altering  
the view on the page.

When it does validate correctly, the message is cleared and flash  
notices are discarded on subsequent page redirects.
