Static pages in Ruby on Rails

后端 未结 9 1042
终归单人心
终归单人心 2020-12-04 05:47

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

  • Home
  • About
  • Contact

I would appric

相关标签:
9条回答
  • 2020-12-04 06:01

    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.

    0 讨论(0)
  • 2020-12-04 06:02

    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.

    0 讨论(0)
  • 2020-12-04 06:06

    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.

    0 讨论(0)
  • 2020-12-04 06:11

    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.

    0 讨论(0)
  • 2020-12-04 06:15

    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
    
    0 讨论(0)
  • 2020-12-04 06:16

    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.

    0 讨论(0)
提交回复
热议问题