0

I'm trying to use a non-controller-connected button to insert a rails partial in a div. As I'm not doing this via the controller, I can't use the standard format.js to do this. The script is placed inside the assets/javascripts folder and this is the important code inside of it:

accounts.js.erb:

$("#account-div").append("<%= j render("newbutton") %>");

When I'm trying to open the site, I get the following error:

undefined method `render' for #<#<Class:0x007fbc7bd060f8>:0x007fbc7aaa0e40>

The code above works when its placed inside the AJAX response via the respond.to block. How can I pass the correct path so that the render method finds the _newbutton.html.erb file, which resides inside the views/accounts/ folder? Thanks!

Linus
  • 4,643
  • 8
  • 49
  • 74

1 Answers1

1

Since assets cannot use render method, move your .js.erb files into views directory and use

<%= j render(:partial => 'accounts/newbutton') %>

Reference - Rendering partial in js.erb file and https://stackoverflow.com/a/6309970/184184

Community
  • 1
  • 1
Raj
  • 22,346
  • 14
  • 99
  • 142
  • forgot, you need to use `:partial` – Raj Mar 30 '14 at 11:00
  • views/accounts/newbutton should be accounts/newbutton since accounts is the controller directory – Raj Mar 30 '14 at 11:06
  • I think this needs to be checked again – Raj Mar 30 '14 at 11:07
  • Still doesn't work. When rendering the same partial with an AJAX response it works. Could the problem be somewhere else? – Linus Mar 30 '14 at 11:09
  • read this - http://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render – Raj Mar 30 '14 at 11:09
  • Hmm, what is the correct way to load the js file from the views directory? – Linus Mar 30 '14 at 11:16
  • 1
    I think you can place this .js.erb file under /views/accounts and call from your action view. this should help http://stackoverflow.com/questions/18437282/how-to-include-js-erb-file-in-view-folder – Raj Mar 30 '14 at 11:18