I'm using Ruby on Rails 3.1 and I'm wondering how I could render a partial in a javascript asset.
What I'm aiming at:
# in /app/assets/javascript/cart.js.coffee.erb
$('a.add_sth').click -> $('.random_container').append('<%= render partial: 'way/to/partial' %>')
This causes a NoMethodError:
undefined method `render' for #<#<Class:0x007fc54584c6e8>:0x007fc5474cd470>
If I write <%= 2+3 %>
instead it works fine, btw.
I think the problem is that the asset pipeline is independent from the default ActionView and that's why render()
is unknown there. Anyway, is there a way to get that partial's content rendered?
render
puts a new line at the end and makes js code invalid. Fixed by addingchomp
afterrender
– Moramorabito