4

If it is possible, how can I run it from another js.erb-file?

Trying to keep things DRY!

EDIT: Explanation: I have a js.erb routine in search/index.js.erb. I want to use the same routine in location/index.js.erb. Therefore I wondered if I could make a partial containg js.erb code, so that I don't have to copy the routine from the first index.js.erb file to the other one.

Johan Hovda
  • 855
  • 2
  • 10
  • 23

2 Answers2

7

YES. you can have a common partial (say in a folder: app/views/partials) and use that partial's path inside your *.js.erb file like this <%= render :partial => path_to_partial, :locals => { ....} %>

If you want to extract the html string out of a partial you can also use

render_to_string :partial => path_to_partial
Sully
  • 14,672
  • 5
  • 54
  • 79
samdeV
  • 2,158
  • 2
  • 11
  • 12
4

Make sure your rendered file begins with an underscore (i.e.:

if you intend to

<%= render :partial => 'myroutine.js.erb', :locals => { ....} %>

the file should be named _myroutine.js.erb)

nmasna
  • 41
  • 1