We operate o...">

Interpreting Ruby into a Variable

This is a hard question for me to even ask, so I pass the code first:

        <div class="col-xs-12">
          We operate on a pay what you want model. Keep in mind that 
          <% if action_name == "outdoors" %>
            <%= outdoors_donation.html_safe %>
          <% elsif action_name == "snowsports" %>
            <%= snowsports_donation.html_safe %>
          <% end %>
          Below, you'll find information on how we calculated your order suggested donation of $<span id="modal-sentence"></span>.
        </div>

      

Instead of a clumsy if statement, I could just simply inject action_name directly into the variable being called ... something like

<%= "#{action_name}_donation".html_safe %>

      

But I'm not sure what it will actually be

Thank!

+3


source to share


1 answer


You can use send

in ruby ​​to dispatch a method for which you specify a name. This allows for interpolation:

<%= self.send("#{action_name}_donation").html_safe %>

      



In this context self

will be an instance of the view, which will be the same as a call outdoors_donation.html_safe

or snowsports_donation.html_safe

or whatever.

+2


source







All Articles