Ruby on Rails 中的静态页面 [英] Static pages in Ruby on Rails

查看:31
本文介绍了Ruby on Rails 中的静态页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作具有以下页面的 Ruby on Rails 应用程序的标准方法是什么

What are the standard way of making a Ruby on Rails application that will have pages such as

  • 首页
  • 关于
  • 联系方式

如果有人有链接或答案,我会应用程序而不是仅仅说使用 gem,因为我想学习如何使用这种行为制作简单的 web 应用程序.

I would appricate if someone had links or an answers rather than just say use a gem because I want to learn how to make simple webapps with such behavior.

推荐答案

取决于您希望如何处理这些页面中的内容.

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

方法 1 - 在视图中存储内容

如果你只是想把所有的内容放在 ERB 视图中,那么一个非常简单的方法是创建一个 PagesController,其目的是处理静态页面.每个页面由控制器中的一个动作表示.

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:

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'

然后在 app/views/pages 下创建 home.html.erb、about.html.erb 和 contact.html.erb 视图.这些视图包含您想要在静态页面上显示的任何内容.默认情况下,它们将使用您应用的 application.html.erb 布局.

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.

方法 2 - 将内容存储在数据库中

我使用的另一种方法是为静态页面制作一个非常基本的 CMS.在这种情况下,页面在模型中表示.它使用 friendly_id gem 来处理每个页面的 slug,这样它们就可以在URL(例如,/about)而不是 ID.

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:

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:

show.html.erb:

<%= raw @page.content %>

routes.rb:

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

注意:将此条目放在 routes.rb 的末尾,因为它匹配所有内容.

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.

这篇关于Ruby on Rails 中的静态页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆