Render :update returning null

I wondered if anyone can help me, I am trying to add some basic ajax to
my new project but I’ve run into a problem.

I have a RESTful ProductsController which uses pagination, I would like
to basically re-render the index template on susbequent ajax calls to
the index action. However the following doesn’t work

format.js do
render (:update) do |page|
page.replace_html ‘main’, :template => ‘index’
end
end

The response I receive is

Element.update(‘main’,null);

which has the effect of empty the contents of my ‘main’ div, which is
not very helpful. It seems the following works, but its not what I need.

page.replace_html ‘main’, :partial => ‘somepartial’

Any one have any ideas, do I just have the syntax wrong?

I thought replace_html receives a :partial option, and not a :template
option that is unknown… I think the nil object happens because there
isn’t :partial element as argument.

I think you’re right :slight_smile: Unforunately I need to re-render the whole
template (minus the layout) not just a partial. Without separating the
content of my template into a partial for the sake of making this work
(which feels very wrong) I can’t see a way around it.

Any ideas?

Eduardo Yáñez Parareda wrote:

I thought replace_html receives a :partial option, and not a :template
option that is unknown… I think the nil object happens because there
isn’t :partial element as argument.

On 27 Aug 2008, at 08:34, Rob L. wrote:

I think you’re right :slight_smile: Unforunately I need to re-render the whole
template (minus the layout) not just a partial. Without separating the
content of my template into a partial for the sake of making this work
(which feels very wrong) I can’t see a way around it.

This is quite disgusting but:
in your controller @content = render_to_string :template => ‘foo’
in your rjs file : page.replace_html ‘main’, @content

However you don’t need to do this, eg

link_to_remote ‘click me’, :url => …, :update => ‘main’

Then in you action just do a bog standard render :template => …

Fred

Indeed it is disgusting, but sometimes that’s necessary to solve a
problem :frowning:
However I’ve managed to solve it, typically it was easier than I thought

format.js do
render :update do |page|
page.replace_html ‘main’, :file => ‘products/index’
end
end

Yay for the Rails source.

This is quite disgusting but:
in your controller @content = render_to_string :template => ‘foo’
in your rjs file : page.replace_html ‘main’, @content

However you don’t need to do this, eg

link_to_remote ‘click me’, :url => …, :update => ‘main’

Then in you action just do a bog standard render :template => …

Fred

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