1

I can't find how to display a dynamic label in Rails, i've tried using the :value => show_name property but it didn't work, it only displays Show name. Here is the view code

<p>
   <div class="control-group">
           <%= f.label :show_name, :value => :show_name, :class => 'control-label' %> 
           <%= #this next line fails with undefined method `show_name' for #<ActionView::Helpers::FormBuiler>
              #f.label f.send :show_name, :class => 'control-label'
           %> 
       <div class="controls">
           <%= f.text_field :variable_value, :class => 'text_field' %> 
           <%= f.hidden_field :variable_id, :class => 'text_field' %> 
       <%= f.hidden_field :show_name, :class => 'text_field' %> 
       </div>
   </div>
<p>

and if needed here is the show_name definition inside my model.

  def show_name
    Variable.find_by_id(self.variable_id).name
  end
KoU_warch
  • 2,160
  • 1
  • 25
  • 46

2 Answers2

1

Ok, so i end up finding a solution that is very DRY, thank to this post. And the only thing im going to do is explain a lit bit more what to do:

First we are going to asume the most complex case in which we have nested forms and so we are using fields_for inside a form_for method:

  <!-- f represents the form from `form_for` -->
  <%= f.fields_for :nested_model do |builder| %>
    <p>
      <div class="control-group">

         <!-- here we are just calling a helper method to get things DRY -->

         <%= builder.label return_value_of_symbol(builder,:show_name), :class => 'control-label' %> 
         <div class="controls">
            <%= builder.text_field :variable_value, :class => 'text_field' %> 
            <%= builder.hidden_field :variable_id, :class => 'text_field' %> 
         </div>
      </div>
    </p>
  <% end %>

Note that we included the builder object(as specified in the fields_for call) in the parameters of our helper.

In our helper we define the return_value_of_symbol function

  def return_value_of_symbol(obj,sym)
    # And here is the magic, we need to call the object method of fields_for
    # to obtain the reference of the object we are building for, then call the
    # send function so we send a message with the actual value of the symbol 
    # and so we return that message to our view.  
    obj.object.send(sym)
  end
Community
  • 1
  • 1
KoU_warch
  • 2,160
  • 1
  • 25
  • 46
0

Use label_tag, put the show_name on a instance variable on your controller and use like this:

<%= label_tag @show_name, nil, :class => 'control-label' %>

EDIT:

On your application_helper.rb, create a helper method similar to this one:

def show_name(name)
  content_tag(:label, name, :class => 'control-label')
end

Then you can use the show_name(name) on your views like this:

<%= show_name(@name) %>

Just remember to populate the @name variable.

MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • would this be DRY? i would need to add this to every definition in the controller, or at least index and show or anyone else that uses _form.html.erb, i will need to use `<%=` in any case i presume – KoU_warch Nov 19 '12 at 16:13
  • Use a helper then. Put the code in a helper and use it in all of your views. – MurifoX Nov 19 '12 at 16:15
  • you mean like create an alternative to label_tag or somehow return the string of the variable? – KoU_warch Nov 19 '12 at 16:23
  • Yeah. On your application_helper, create a method that receives a name and dinamically create a label using `content_tag`. – MurifoX Nov 19 '12 at 16:25
  • im still not getting how to do that, im doing `<%= content_tag(:label,:show_name, :class => 'control-label') %>` but is the same thing – KoU_warch Nov 19 '12 at 16:35
  • I added an example to my answer. Take a look and see if it helps. – MurifoX Nov 19 '12 at 17:06
  • populating `@name` would mean to do something in the controller? because we would be where we started – KoU_warch Nov 19 '12 at 17:25
  • The variable have to be populated somewhere. You can use the query on the helper too. – MurifoX Nov 19 '12 at 17:28
  • Hey i just found a way without doing a search in the model, hopefully DRY solution, let me know what do you think. – KoU_warch Nov 19 '12 at 20:09