3

I am working on a basic Rails application (Rails version 4.1) and I would like to include a static page in it. I have got the static page with following directory structure:

| |- index.html |- css folder |-- (some css files) |- js folder |-- (some js files) |- images folder |-- (some images)

How can I include it in my application? I intend to make it my homepage btw.

I have tried using the 'layout' keyword in the controller by including the html file in the 'app/views/layout' directory. However, I have at-most been able to render the 'index.html' file but it misses any styling i.e. gets rendered as plain text. I have been able to obtain the same result by using the HighVoltage gem.

5
  • If it's only a static page then i think you'll be better of using high_voltage gem. Can you explain what issues you had while using it? Commented Sep 14, 2014 at 8:07
  • Why can't you place all of your css, js and images in the assets pipeline as usual? Commented Sep 14, 2014 at 8:55
  • @Mandeep As I wrote, I have only been able to render the page only as a plain text i.e as a black and white sheet, with variations only in size (like a .txt file). From what it looks to me, it seems to not be including the assets that came along with it. I have tried putting them alongwith it in the 'pages' folder meant for the HighVoltage gem, and in the app/assets folder. Commented Sep 14, 2014 at 8:59
  • @AdityaBhardwaj ahh okay! I see so high_voltage doesn't allow you to use a layout file where you could load your stylesheets and js Commented Sep 14, 2014 at 9:01
  • @LcLk I tried that, but it doesn't help. Commented Sep 14, 2014 at 9:11

2 Answers 2

5

Page

When you mention a "static" page -- all the pages are static in Rails

Rails just renders HTML, using the data from your database. Much like PHP (which many developers can appreciate), Rails is a processor which allows you to populate your view with the data you need.

This means that if you want to make a "static" page, you just need to not pull any data from the db:

#config/routes.eb
root: "application#home"

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def home
   end
end

This will give you the ability to load the following view:

#app/views/application/home.html.erb
Put stuff here

Assets

Now, the slightly trickier part is to include the right assets for this view.

You mention you have your own directory structure. Let me tell you now that you'll be MUCH better served by creating & serving assets from your assets pipeline.

Here's how:

#app/views/layouts/application.html.erb
<head>
  ...
  <% if controller_name == "application" && action_name == "home" %>
     <%= stylesheet_link_tag "welcome" %>
     <%= javascript_include_tag "welcome" %>
  <% end %>
</head>

This will give you the ability to call the following:

#app/assets/stylesheets/welcome.css
...

#app/assets/javascripts/welcome.js
...

Finally, if you then add the "welcome" files to your asset precompilation list, it should work for you:

#config/application.rb
Rails.application.config.assets.precompile += ['welcome.js', 'welcome.css']
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to avoid having to add each file to the precompile list?
0

Since you have custom styles and js for your home page so you'll need to do to it more any other view. Create a controller for your home page, call it may be pages_controller. Create a custom action, lets say home, make a route for this action and then place your html inside this actions view

Follow these steps to make it work:

  • Create a controller:

rails g controller Pages

  • Create a action inside your pages_controller.rb

def home;end

  • Create a route for your action

root :to => 'pages#home'

This will by default use application.html.erb as your layout and inside your application.html.erb you have this line

<%= stylesheet_link_tag "application", media: "all" %> #this by default load all the css from assets/stylesheets/application.css so you can require your css file inside it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.