Topic: RJS file runs but page doesn't update

I've got what I think is a simple bit of Ajax/rjs which won't work.  I've got something very similar working elsewhere but for some reason this won't.  According to Firebug the rjs code is being run but the page itself isn't updating.  Here's the various bits of related code:


<h3>Tip list</h3>
<div id="tips">
<%= render  :partial => 'tip', :locals => {:bgcolor => "#ccc"}, :collection => @tips %>   
<%= render :partial => 'new' %>


<h3>Submit a new tip</h3>
<% form_remote_for :tip,, :url => {:controller => 'tip', :action =>  'add'}, :html => {:id => 'tip_form'} do |f| %>
<p><label for="tip_name">Tip</label><br />
<%= f.text_field 'name' %></p>
<p><label for="tip_description">More Info</label><br />
<%= f.text_area 'description' %></p>
<%= submit_tag 'Add' %>
<% end %>


def add
  @tip = Tip.create(params[:tip])
  @tips = Tip.find(:all)


page.replace_html :tips, {:partial => 'tip', :locals => {:bgcolor => "#ccc"}, :collection => @tips }

My development logs show that the partial 'tip' is being rendered when I click on the "Add" button.  Firebug shows a response of: Element.update("tips", "..."); where the "..." is the rendering of the partial.  And yet nothing gets replaced... I've tested this in both Firefox and IE.

Any ideas?

Last edited by Draconid (2007-04-02 16:58:46)

Re: RJS file runs but page doesn't update

Try running your XHTML source through a validator. If there's a missing close tag or something, Javascript will have a hard time replacing the content of the "tips" div.

Railscasts - Free Ruby on Rails Screencasts

Re: RJS file runs but page doesn't update

It wasn't valid, but I've now got it validating fine and it still isn't working.  hmm

Re: RJS file runs but page doesn't update

I wonder if it has something to do with javascript trying to replace the form that originally called it. I don't see why this would be a problem, but you might want to try temporarily moving the form outside of the tips div just to see if it works then.

The only other thing I can think of is perhaps the prototype javascript files are old. If you are dealing with an older project, try creating a new empty rails project and copying those javascript files to your current one.

Railscasts - Free Ruby on Rails Screencasts

Re: RJS file runs but page doesn't update

Aha!  Fixed it!

I remembered seeing a little while back about someone having problems with two rhtml files of the same name - one a partial and one not.  When you call a partial from a normal rhtml file, even if there's a normal rhtml file of the same name (in this case new.rhtml and _new.rhtml) it knows to use the partial.  For some reason when you call a partial from an rjs it doesn't actually use the partial but the non-partial - so while it's rendering fine because it's not actually a partial it's not displaying.  Or something like that.

So I've renamed new.rhtml to new2.rhtml (temporarily - I'll be renaming _new.rhtml longterm but this was quicker for testing) and now, all of a sudden, it works fine!

Thanks for the input though.. I'm glad I validated the xhtml as I'm sure that would have caused problems longterm!

Re: RJS file runs but page doesn't update

Okay, that's weird.  While renaming new.rhtml has definitely solved the problem, I've just realised that the partial being called is 'tip' so I'm still none the wiser about why it happened...

Re: RJS file runs but page doesn't update

That's very strange, I wouldn't think there would be a conflict between a partial and a template with the same name since a partial is distinguished with the underscore character. Oh well, at least it's working.

Railscasts - Free Ruby on Rails Screencasts