San Backups
San Backups

Reputation: 503

Ruby variable each loop substitution

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.

Upvotes: 0

Views: 171

Answers (1)

tokland
tokland

Reputation: 67900

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 %>

Upvotes: 1

Related Questions