Search code examples
ruby-on-railspartialrjs

render collection of partials as html in rails


I am using jQuery to do an ajax call to my controller, and everything is working perfectly, except I can't quite get the return value that I want.

Let's say I have a collection of items, and I have the partial:

/app/views/messages/_message.html.erb

I am trying to render it as a big string of text/html like so:

format.js { render_to_string :partial => "message", :collection => @messages}

However, this is rendering a view of some sort, because I'm getting and tags. Even if I change it to:

format.js { render_to_string :partial => "message", :collection => @messages, :layout => 'none'}

it still returns the same thing. Is this possible? I need to return it for jQuery to handle it, so I'm not sure if this can be done with rjs. Any help is appreciated.


Solution

  • Have you tried just using render instead of render_to_string?

    Also, the fact that you're using jQuery makes this slightly more complicated, if you were using prototype you could use rjs and do something like

    page.replace_html('container_element', :partial => @messages)