24fc194843a71f10949be18d5a692682

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

<%= 'class="current"' if controller.action_name=='index' || controller.action_name=='players' %>

Refactorings

No refactoring yet !

8f6f95c4bd64d5f10dfddfdcd03c19d6

Rick DeNatale

April 24, 2009, April 24, 2009 11:55, permalink

2 ratings. Login to rate!
<%= 'class="current"' if %w[index players].include?(controller.action_name) %>
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 24, 2009, April 24, 2009 14:46, permalink

1 rating. Login to rate!

Or if you were not using lame rails and had extlib:

<%= 'class="current"' if controller.action_name.in? %w( index players )  %>
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 24, 2009, April 24, 2009 14:48, permalink

No rating. Login to rate!

Most likely you would move this method to the controller though and maybe just do:

<%= 'class="current"' if controller.current? %>
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 24, 2009, April 24, 2009 14:50, permalink

1 rating. Login to rate!

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

D41d8cd98f00b204e9800998ecf8427e

bluehavana

May 2, 2009, May 02, 2009 22:13, permalink

1 rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel