Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:42:06+00:00 2026-05-23T22:42:06+00:00

Is it possible in Rails 3.0 to render a partial where the name of

  • 0

Is it possible in Rails 3.0 to render a partial where the name of the partial is stored in a variable?

I am attempting something similar to:

<%= escape_javascript(raw render :partial => @partial_name, :locals => { :value => @value} )%>

Updated with more details

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’.

Each of these models have their own custom partial for displaying information and follow the naming scheme: ‘_awesome_cupcake’ and ‘_alright_cupcake’.

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’.

Here is a little more information into how my components are laid out:

application.js

$('#set_cupcake').click(function () {
   $.getScript('/cupcakes/new.js?cupcake_type=awesome');
});

cupcakes_controller.rb

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

views/cupcakes/new.js.erb

$('#cupcakes')
  .append("<%= escape_javascript(raw render :partial => @partial_name, :locals => { :cupcake => @cupcake } ) %>")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:42:07+00:00Added an answer on May 23, 2026 at 10:42 pm

    So I finally figured out what the issue was. In my controller I was setting the @partial_name within the scope of a condition that was checking if a specific model existed.

    I did not realize that using Object.const_defined? doesn’t really work for rails in development mode as the models aren’t initially all loaded. An alternate approach I found is to search through a list of database table connections using ActiveRecord::Base.connection.tables.

    However I think a better solution is to store some file that ‘registers’ acceptable values for this parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I render a view within another view... in rails... is it possible?
So, you can do something like that in rails: @features.each do |feature| render feature
Is it possible to render a partial from inside a ruby script or from
Is it possible to have rails render html into a hash instead of to
is it somehow possible to call a rails function or to access a rails
Is it possible to integrate MS Outlook/Exchange tasks with Rails? I know how to
Is it possible to use single XML file for Ruby on Rails as an
Is is possible to create a recursive route in Rails? I have an application,
Is it possible to have a single Ruby on Rails installations have multiple applications,
In Ruby on Rails, is it possible to change a default action for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.