Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

How to change the background on each page in rails?

So I am using Ruby on Rails to build a collaborative artist platform. I would like the homepage's body background to be yellow, and for example the body of the artist index page to be blue and let's say the body of the artwork index page to be red. In application.thml.erb I've set <body class="<%= 'background_yellow' if controller.action_name == 'home' %>"> but that would work only if the homepage's background would be yellow and all the other pages would be white. But here I want each individual page to be a different color. How can I do that?

question from:https://stackoverflow.com/questions/66063074/how-to-change-the-background-on-each-page-in-rails

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way is to add a class with the controller name somewhere in your HTML code.

<body class="<%= "controller_#{controller.name}" %>">

then declare some CSS classes in your style-sheet file

.controller_welcome {
  background-color: yellow;
}

.controller_users {
  background-color: pink
}

/* and so on */


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...