Variable inside of another variable

Hi everyone,

I'm building partial and I need to use a variable inside of another variable name:

I've tried like this:

  <div class="form_row">     <label for="<%= field %>"><%= field_title || field.humanize %>:</

    <%= form.text_field field,                         :size => User::field.upcase_SIZE,                         :maxlength => User::field.upcase_MAX_LENGTH %>   </div>

like this:

  <div class="form_row">     <label for="<%= field %>"><%= field_title || field.humanize %>:</

    <%= form.text_field field,                         :size => User::#{field.upcase}_SIZE,                         :maxlength => User::#{field.upcase}_MAX_LENGTH %>   </div>

and like this:

  <div class="form_row">     <label for="<%= field %>"><%= field_title || field.humanize %>:</

    <%= form.text_field field,                         :size => User::{field.upcase}_SIZE,                         :maxlength => User::{field.upcase}_MAX_LENGTH %>   </div>

To help you understand what I want:

I will call the partial on my view using this code: <% render :partial => "text_field_row", :field => "email", :locals => { :form => form } %>

and what I want to be displayed in this case is:

  <div class="form_row">     <label for="email">Email:</label>     <%= form.text_field :email,                         :size => User::EMAIL_SIZE,                         :maxlength => User::EMAIL_MAX_LENGTH %>   </div>

but I will also use it for another fields rather than just email.

In PHP I know I could simply do it by ${all_${name}_text} but how does it work with ruby on rails?

Thanks!

Hi everyone,

I'm building partial and I need to use a variable inside of another variable name:

and what I want to be displayed in this case is:

<div class="form_row">    <label for="email">Email:</label>    <%= form.text_field :email,                        :size => User::EMAIL_SIZE,                        :maxlength => User::EMAIL_MAX_LENGTH %> </div>

but I will also use it for another fields rather than just email.

you probably want to look at const_get.

Fred

Thanks Fred!

I got it working wonderfully with:

  <% field_title = nil if not defined?(field_title) -%>   <div class="form_row">     <label for="<%= field %>"><%= field_title || field.humanize %>:</

    <%= form.text_field field,                         :size => User::const_get(field.upcase + "_SIZE"),                         :maxlength => User::const_get(field.upcase + "_MAX_LENGTH") %>   </div>