Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a partial in assets

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?

like image 293
csch Avatar asked Sep 07 '11 15:09

csch


People also ask

How do you render a partial?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What are partials in Rails?

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.

How can you tell Rails to render without a layout?

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.

What does render do in Rails?

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.


1 Answers

Bad news, render is not available Look: same question on GitHub

like image 90
link_er Avatar answered Sep 21 '22 00:09

link_er