Take the 2-minute tour ×

I have 2 view files, index.html.erb and index.js.erb. The erb file obviously contains all the data to be sent to the yield call.

Now I want to insert the content of index.html.erb into #contents div on the page, using index.js.erb.

Is there a way to render index erb file into a variable in the js.erb file, so I can send it to the client? Currently I'm having to create a partial which contains the complete html.erb file, and calling that from both the main views.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

In your index.js.erb (here example for post resource):

<%= render template: "posts/index.html" %>
<%= render template: "posts/index.html" layout: false %>

Using render is quite good described in rails guides.

share|improve this answer
Yes! Just found it when searching for code (file: ""). Do you know if there's any difference between file: and template: ? Both work the same for me. –  user1527166 Aug 11 '12 at 21:48
Actually with template: you get content-type 'text/html' by default and with file: you can specify any including custom. –  Voldy Aug 11 '12 at 21:53

Your Answer


