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?
You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.
By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.
You've learned about the all-important topic of rendering in Ruby on Rails. Rendering allows you to return content to the users of your application & it's one of the final steps inside your controller actions.
Bad news, render is not available Look: same question on GitHub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With