Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic body id

How can I set ID for body tag in Rails?

<body id="login">
like image 339
ceth Avatar asked Apr 15 '11 11:04

ceth


People also ask

How to add id in PHP?

You need to enclose (wrap) your PHP output in a div . And then give id to that div. Also, no need for brackets ( and ) for echo as it is a language construct, not a function.

Can you put ID on body HTML?

In HTML5, the id attribute can be used on any HTML element (it will validate on any HTML element.


1 Answers

I don't find any of the solutions particularly elegant or flexible. Add a body_class method to application_helper.rb:

  def body_class
    [controller_name, action_name].join('-')
  end

..in layout:

<body class="<%= body_class %>">
#e.g output: <body class="articles-show">

You can then refine the above helper to dynamically inject other identifiers such as modules.

like image 78
Damien Roche Avatar answered Oct 30 '22 04:10

Damien Roche