4

in index.html.erb, I would like to know how to insert index.js.erb, something like:

<script type="text/javascript">
$(function() {
    <%= render =>"home/index.js.erb" %>
});
</script>

Any ideas on how to just insert the index.js.erb file ?

thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

2 Answers2

5
<%= File.read "#{RAILS_ROOT}/app/views/home/index.js.erb" %>
cam
  • 14,192
  • 1
  • 44
  • 29
  • 1
    Instead of using RAILS_ROOT, use ::Rails.root `::Rails.root.join('public','images','logo.png')` (as per [this answer](http://stackoverflow.com/questions/9192795/rails-root-not-longer-valid-when-loading-images-with-the-prawnto-2-gem)) – Geir Freysson Dec 08 '12 at 09:24
1

Set your file as a partial and append preprend or do what you wana do with it.

For a partial file named _index.js.erb located in the same folder of where the file with this code is called will be prepend in the DOMID exemple.

jQuery('#exemple').prepend("<%= escape_javascript(render(:partial => 'index')) %>");

Maybe you can do a html file with it...

<% javascript_tag do %>

<%= render :partial => 'index' %>

or direct code

<% end %>
4nkh
  • 11
  • 2