Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3807908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:03:54+00:00 2026-05-19T15:03:54+00:00

In PHP there’s a clever trick where, based on the URL of a page,

  • 0

In PHP there’s a clever trick where, based on the URL of a page, you can automatically assign a unique ID to the body tag of every page on your site. (which i find super helpful in writing CSS).

e.g.

<?php
        $url = explode('/',$_SERVER['REQUEST_URI']);
        $dir = $url[1] ? $url[1] : 'home';
?>

<body id="<?php echo $dir ?>">

-> source: http://css-tricks.com/snippets/wordpress/id-the-body-based-on-url/

The Result being that,

  • a page with a url of “http://domain.tld/blog/home&#8221; gets a body with an ID of “blog”, and
  • a page with a url of “http://domain.tld&#8221; gets an ID of “home”.

I’d like to know if there’s an, (ideally) equally simple and elegant, way to do the same in a Ruby on Rails based website?

Thanks so much for sharing your wisdom, oh great mucky mucks of code!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:03:55+00:00Added an answer on May 19, 2026 at 3:03 pm

    You could name it by controller and action by adding adding the following in application_controller.rb:

    before_filter :create_body_id
    def create_body_id
      @body_id = "#{params[:controller]}-#{params[:action]}"
    end
    

    Then add this in the template: id="<%= @body_id %>"

    For the index action in blog_controller you’d get id="blog-index"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In php there's a function called uniqid . It generates unique identifiers based on
This is the url of my page : http://www.animalswecare.com/Ads/postad.php There are two fields category
In php there is a list of all the parameters you can pass to
I'm trying to write some command line PHP - is there a trick to
Active site can be seen here: http://www.studioimbrue.com/index2.php There are currently two small problems with
I have two page: rooms.php and reservation.php User can only access to reservation.php via
I see that in PHP there are functions that can accept multiple options into
In PHP there's a function called stream_wrapper_register . With that i can get the
I'm a rookie designer having a few troubles with this page: http://www.resolvegroup.co.nz/javasurvey.php There are
I heard that in PHP there are some alternatives for the functions substr() and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.