Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do static content in Rails?

Looking at different options:

One is to just put the static pages in the public/ folder, but I do want the header from layout/application to be consistent.

I tried this, but I got an error:

# in routes.rb: map.connect '*path', :controller => 'content', :action => 'show'  # in content_controller.rb: def show   render :action => params[:path].join('/') end 

All I want is an easy way to put together things like my faq, contact, tos, privacy, and other non-application type pages somewhere easy by just creating an .rhtml. who has done this?

like image 872
Satchel Avatar asked Jul 18 '09 03:07

Satchel


People also ask

What is static pages in Rails?

Roll Your Own Static Pages in a Rails App. Have you ever wanted to create static pages in a Rails application? These are pages which don't necessarily contain any dynamic info or pull from the database, and don't require an entire controller.

What is static content in app?

App Engine can serve static content such as HTML pages and media such as images. Static content is anything that will not be executed as JSPs or Servlets. The following example is a basic HTML page that shows a welcome message.

What is static content in Java?

Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.


1 Answers

For Rails6, Rails5 and Rails4 you can do the following:

Put the line below at the end of your routes.rb

  get ':action' => 'static#:action' 

Then requests to root/welcome, will render the /app/views/static/welcome.html.erb.

Don't forget to create a 'static' controller, even though you don't have to put anything in there.

Limitation: If somebody tries to access a page that does not exist, it will throw an application error. See this solution below that can handle 404s

For Rails3 you have to use 'match' instead of 'get'

  match ':action' => 'static#:action' 
like image 108
Roland Studer Avatar answered Sep 19 '22 08:09

Roland Studer