17
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails+Bootstrapでactiveなnavbarを自動で設定

Last updated at Posted at 2015-02-12

Railsでbootstrapのnavbarを使用する際に、自動的にactiveなliタグを設定する際は以下のように記述する。

<ul class="nav navbar-nav">
  <li class= "<%= 'active' if controller.controller_name == 'users' %>"><%= link_to "ユーザ", { controller: "users" } %></li>
  <li class= "<%= 'active' if controller.controller_name == 'books' %>"><%= link_to "本", { controller: "books" } %></li>
</ul>

Slimの場合は以下のように記述。

ul.nav.navbar-nav
  li class = ("active" if controller.controller_name == "users")
    = link_to "ユーザ", { controller: "users" }
  li class = ("active" if controller.controller_name == "books")
    = link_to "本", { controller: "books" }

参考

17
21
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?