Ruby on Rails 中的静态页面

制作 Ruby on Rails 应用程序的标准方法是什么

  • 回家
  • 关于
  • 接触

如果有人有链接或答案,我会很感激,而不是只说使用一个宝石,因为我想学习如何使这样的行为简单的网络应用程序。

33192 次浏览

Depends on how you want to handle the content in those pages.

Approach #1 - store content in views

If you just want to put all your content in ERB views, then a very simple approach is to create a PagesController whose purpose is to deal with static pages. Each page is represented by one action in the controller.

pages_controller.rb:

class PagesController < ApplicationController
def home
end


def about
end


def contact
end
end

routes.rb:

match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'

Then create home.html.erb, about.html.erb, and contact.html.erb views under app/views/pages. These views contain whatever content you want on your static pages. They'll by default use your app's application.html.erb layout.

You'll also want to look into page caching to give yourself a boost in performance.


Approach #2 - store content in database

Another approach I've used is to make a very basic CMS for static pages. In this case, pages are represented in the model. It uses the friendly_id gem to handle slugs for each page so that they can be retrieved by a pretty name in the URL (e.g., /about) rather than by ID.

page.rb:

class Page < ActiveRecord::Base
attr_accessible :title, :content


validates_presence_of :title, :content


has_friendly_id :title, :use_slug => true, :approximate_ascii => true
end

pages_controller.rb:

class PagesController < ApplicationController
def show
@page = Page.find(params[:id])
render 'shared/404', :status => 404 if @page.nil?
end
end

show.html.erb:

<%= raw @page.content %>

routes.rb:

match '/:id' => 'pages#show'

Note: put this entry at the end of routes.rb since it matches everything.

Then how you want to create, edit and update pages are up to you - you can have an admin interface, or build it in to your public interface somehow. This approach can benefit from page caching too.

An adequate answer to your question would basically look like an introduction to the Rails framework: the MVC structure, templating, and routing DSL at least. Jeff has given a good stab, but his answer still assumes a lot of basic Rails knowledge on your part.

I'd suggest though, that if your webapp is really that simple, Rails might be overkill. I'd look into something lighter, like Sinatra, which has a much lower learning curve than Rails and does a great job of this kind of thing without having to deal with complex routing, magic MVC action/template mapping, etc.

Check out Michael Hartl's http://railstutorial.org which comes in a 2.3.8 and 3.0.x version. It covers this with great examples and leads you through building them very early on and you will also have the opportunity to learn a lot more than this example. I highly recommend it.

Another option is the high_voltage gem: https://github.com/thoughtbot/high_voltage

This makes it super easy to create static pages where the content is stored in views.

For more you can create static pages using Jekyll bootstrap or also Jekyll using Danger blog

Refer it is very helpful.

I'd suggest adding your pages in the public folder so as to be served directly without having to pass through rails at all. I'm not an expert though so I'm not sure if this could have any cons if the page is static.

Jeff's Approach #1 works great for me. Here is a trick to make the controller dynamically look up pages. With this, you don't need to touch the controller nor the routes.rb for adding pages. Just drop the pages under app/views/pages and the controller will find it.

class PagesController < ApplicationController
def show
render params[:id]
end
end

Jeff's approach #1 (storing content in views and having a route and controller action for each static page) is a good one. The only thing I would add is to use the controller macro in your routes.

So, instead of this:

match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'

You can do this:

controller :pages do
get :home
get :about
get :contact
end

It's two extra lines, yet much so much more elegant, since it eliminates repetition and groups your static page routes together visually.

It also uses the get http verb method instead of match, which is a better practice for Rails routes (and more concise, now that Rails 4 requires the http verb to be specified when using match.

config/routes.rb

get ':id', to: 'pages#show'

app/controllers/pages_controller.rb

class PagesController < ApplicationController
def show
begin
render params[:id]
rescue ActionView::MissingTemplate
render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
end
end
end

Then place your static pages in app/views/pages/{name}.html.erb (or whatever template format.)