jackerman09
jackerman09

Reputation: 2610

Setting a variable dynamically in rails

Is it possible for me to dynamically determine a field name or variable, e.g.

module ApplicationHelper
def current_week
  1
end

Controller
def @player = Player.find(params[:id]) 

/item/show.html.erb
<%= @player.total_for_week_{current_week} %>

This way I can update the current_week variable, either manually or based on a formula, and have it flow to all of my views. Is there a way to do this?

Thanks!

Upvotes: 0

Views: 35

Answers (1)

gotva
gotva

Reputation: 5998

<%= @player.send("total_for_week_#{current_week}") %>

Upvotes: 2

Related Questions