0

I need to replace X_VARIABLE in 2 places. For the first X_VARIABLE I want to keep the text 'remove' For the second X_VARIABLE I want to keep 'd_cars_path'

<% @cars.each do |x| %>
    <% @a = @b.send(x) %>
    <% if @a == true %>
      <%= button_to "removeX_VARIABLE", X_VARIABLEd_cars_path(:id => @user.id), class: "btn btn-large btn-primary" %>
    <% end %>
<% end %>

I am looking for some help with the variable substitution syntax. Thanks.

1
  • do you want the result like <%= button_to "remove", d_cars_path(:id => @user.id), class: "btn btn-large btn-primary" %> Commented Sep 8, 2012 at 10:37

1 Answer 1

1

I'd write:

<% @cars.each do |x| %>
  <% if @b.send(x) %>
    <%= button_to "remove#{x}", 
          send(:"#{x}d_cars_path", id: @user.id), 
          class: "btn btn-large btn-primary" %>
  <% end %>
<% end %>
Sign up to request clarification or add additional context in comments.

3 Comments

+1 I prefer this approach (and I gave a haml version of it too).
Thank you. I didn't know the send(: ) function.
@Tony you used send in your code :-) maybe you didn't know that you can use it with implicit object (self).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.