Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecifying name of rails partial to render using a variable
    text
    copied!<p>Is it possible in Rails 3.0 to render a partial where the name of the partial is stored in a variable?</p> <p>I am attempting something similar to:</p> <pre><code>&lt;%= escape_javascript(raw render :partial =&gt; @partial_name, :locals =&gt; { :value =&gt; @value} )%&gt; </code></pre> <p><strong>Updated with more details</strong></p> <p>In my application I have a set of models using multi-table inheritance, for example lets say I have a base model 'cupcake' and variants such as 'AwesomeCupcake' and 'AlrightCupcake'. </p> <p>Each of these models have their own custom partial for displaying information and follow the naming scheme: '_awesome_cupcake' and '_alright_cupcake'. </p> <p>On a page I have links to each type of cupcake in which I want the controller to accept a parameter and dynamically derive which partial to render. The following works as wanted if (in the new.js.erb file) I replace '@partial_name' with something such as 'awesome_cupcake'.</p> <p>Here is a little more information into how my components are laid out:</p> <p><strong>application.js</strong></p> <pre><code>$('#set_cupcake').click(function () { $.getScript('/cupcakes/new.js?cupcake_type=awesome'); }); </code></pre> <p><strong>cupcakes_controller.rb</strong></p> <pre><code>def new cupcake_type = "#{params[:cupcake_type].capitalize}Cupcake" if Object.const_defined? cupcake_type @cupcake = cupcake_type.constantize.new @partial_name = "#{params[:cupcake_type]}_cupcake" end end </code></pre> <p><strong>views/cupcakes/new.js.erb</strong></p> <pre><code>$('#cupcakes') .append("&lt;%= escape_javascript(raw render :partial =&gt; @partial_name, :locals =&gt; { :cupcake =&gt; @cupcake } ) %&gt;") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload