user1224344
user1224344

Reputation: 129

Render a string as a variable in rails

I'm building a site wide subnav, see code below:

= link_to "#{params[:controller].capitalize}", {:controller => "#{params[:controller]}", :action => "index"}
- if params[:action] != 'index'
  >
  = link_to "#{params[:action].capitalize.humanize}", {:controller => "#{params[:controller]}", :action => "#{params[:action]}"}
  >
  = "@#{params[:controller]}"

The last line renders:

@products

as a string which I'd like to render as a variable.

Upvotes: 1

Views: 268

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

If you want to render the value of instance variable named '@' + params[:controller], you can use Object#instance_variable_get method:

= instance_variable_get("@#{params[:controller]}")

Upvotes: 2

Related Questions