How to do static content in Rails?

2020-01-27 09:50发布

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?

8条回答
【Aperson】
2楼-- · 2020-01-27 10:04

Lindsaar solution is one of the best I ever seen. He build a caching static pages that expired when git revision changed.

<%= cache "site-page-#{@page_name}-#{App.git_revision}" do %>
  <%= render :partial => @page_name %>
<% end %>
查看更多
SAY GOODBYE
3楼-- · 2020-01-27 10:06

Create a PagesController for your static pages (e.g contact) and insert

def contact_page
end

in config/routes.rb insert

get 'contact' => 'pages#contact_page'

which will display the content from views/pages/contact_page.html.erb

查看更多
放荡不羁爱自由
4楼-- · 2020-01-27 10:07

I used the idea of a generalized controller from the answers given, but I wanted to catch 404s, so I put an action in it to handle either case:

# app/controllers/static_controller.rb
class StaticController < ApplicationController
    def static_or_404
        render params[:static]
    rescue ActionView::MissingTemplate
        render :not_found
    end
end

and then at the very bottom in my routing:

# config/routes.rb
get '*static', to: 'static#static_or_404'

It serves the view from app/views/static of the same name as the path, and if there isn't such a view, it serves app/views/static/not_found.html.erb. One could also replace render :not_found with redirect_to root_path or anything else one wanted to have happen.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-01-27 10:09

thoughtbot has a plugin called high_voltage for displaying static content: https://github.com/thoughtbot/high_voltage

查看更多
走好不送
6楼-- · 2020-01-27 10:12

For 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.

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

  match ':action' => 'static#:action'
查看更多
不美不萌又怎样
7楼-- · 2020-01-27 10:12

Considering if u have 1 Home Controller with couple method like show, aboutus, privacy :

class HomesController < ApplicationController
  def show
  end
  def privacy
  end
  def aboutus
  end
end

And map the show method to your root, and map the other to some named routes like

map.root      :controller => "homes", :action => "show"
map.aboutus "/aboutus", :controller => "homes", :action => "aboutus"
map.privacy "/privacy", :controller => "homes", :action => "privacy"

And with view for each

app/views/homes/aboutus.html.erb --> you get http://localhost:3000/aboutus
app/views/homes/show.html.erb --> you get http://localhost:3000 (root)
app/views/homes/privacy.html.erb --> you get http://localhost:3000/privacy

All using the same layout at app/views/layout/application.html.erb

查看更多
登录 后发表回答