Beginner with Rails 3.1 and “static” pages

2019-09-16 12:40发布

I just started deploying a Rails website. In fact, I started programming on Rails 2 days ago. I've created a new project, added some gems, etc. Everything is working properly, and I have some basic knowledge on how all works.

The thing is that what I want to create is a simple website with some sections (let's say, News, Contact, About, Products...). All this content is kinda static.

But I came in a problem. I don't really know what to do in order to create them. What I want, for example, is something like mypage.com/products/fashionableproduct, mypage.com/about, etc, or even mypage.com/page/products.

I thought about creating a Controller, then an action for each page... afterwards, I came up with other solution: scaffolding. Creating a resource called page, that has a title, etc...

I'm really a beginner on this topic, and I would like to hear your helpful voice. Thanks!

3条回答
ゆ 、 Hurt°
2楼-- · 2019-09-16 12:56

Check out https://github.com/thoughtbot/high_voltage for static pages for Rails.

And check out http://railscasts.com/episodes/30-pretty-page-title for setting page titles.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-16 12:58

I think the title of your post might be a bit misleading. I have the feeling you don't want static pages but some database stored content. Just like Ben Simpson tells you to do, create a normal pages controller and make it work.

In the end you might want to customize some routes to get them to be exactly the way you want as in your examples.

Since you just started the app, I strongly recommend you start over and make a new app with Rails 3.1 which is the most current version and learn how to do the basics through http://guides.rubyonrails.org/ and a few other sources such as http://railscasts.com.

You will then learn Rails the right way from the beginning. Good luck and have fun in the process.

查看更多
冷血范
4楼-- · 2019-09-16 13:02

The paths to your files are determined by your routes. The configuration file for routes is located at config/routes.rb. You can match a URL path, and then point to a given resource. More information about routes here: http://guides.rubyonrails.org/routing.html

If you generate a controller, you can process any dynamic data and then pass this data to these "kinda static" pages. Here is an example configuration that would match the path "mypage.com/about" and display the appropriate page:

# config/routes.rb
match "/about" => "example_controller#about"

# app/controllers/example_controller.rb
class ExampleController < ApplicationController
  def about
    # calculations
  end
end

# app/views/example/about.html.erb
<!-- This is your HTML page -->
查看更多
登录 后发表回答