Ajax request render from RJS and handling Flash notices

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.

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.

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