<%= 'class="current"' if controller.action_name=='index' || controller.action_name=='players' %>
Refactorings
No refactoring yet !
Rick DeNatale
April 24, 2009, April 24, 2009 11:55, permalink
<%= 'class="current"' if %w[index players].include?(controller.action_name) %>
Tj Holowaychuk
April 24, 2009, April 24, 2009 14:46, permalink
Or if you were not using lame rails and had extlib:
<%= 'class="current"' if controller.action_name.in? %w( index players ) %>
Tj Holowaychuk
April 24, 2009, April 24, 2009 14:48, permalink
Most likely you would move this method to the controller though and maybe just do:
<%= 'class="current"' if controller.current? %>
Tj Holowaychuk
April 24, 2009, April 24, 2009 14:50, permalink
OR add a class based on the action name regardless of its state, so the class index or players would be added. Which is a better way to go as far as CSS is concerned, index and players just happen to be what you consider 'current' but you should deal with that at the style level
bluehavana
May 2, 2009, May 02, 2009 22:13, permalink
I would add a function to the model of what ever you are trying to display with the class.
class MyModelFooMigration < ActiveRecord::Migration
def self.up
add_column :foo_table, :css_class, :string
end
def self.down
remove_column :foo_table, :css_class
end
end
# or just use a model function
class Players < ActiveRecord::Base
def css_class
"current"
end
end
<%= 'class=#{@my_player.css_class}' %>
I just love the idea of this website and want to try it out with a super easy snippet.
I'm new to ruby, but I'm sure that this beautifull language offers a better solution than mine