Search code examples
jqueryajaxruby-on-rails-3erbcode-duplication

Rails Jquery rendering js.erb partial from another js.erb file


I have duplicate js.erb code between different model's ajax responses. I would like to refactor the duplicate js.erb code by passing arguments into a js.erb partial.

How do i render a js.erb partial from a js.erb file?


Solution

  • Just like you would render any other partial with erb. Let's say your partial is called _my_partial.js.erb (note partials always start with _), then in your main .js.erb file you would do:

    <%= render 'my_partial' %>
    

    If your partial is in another directory, just use:

    <%= render 'path/to/my_partial' %>