Ruby on Rails 中的静态页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4479233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static pages in Ruby on Rails
提问by LuckyLuke
What are the standard way of making a Ruby on Rails application that will have pages such as
制作具有以下页面的 Ruby on Rails 应用程序的标准方法是什么?
- Home
- About
- Contact
- 家
- 关于
- 接触
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.
如果有人有链接或答案,而不是仅仅说使用 gem,我会很高兴,因为我想学习如何使用这种行为制作简单的 web 应用程序。
回答by Jeff
Depends on how you want to handle the content in those pages.
取决于您希望如何处理这些页面中的内容。
Approach #1 - store content in views
方法#1 - 在视图中存储内容
If you just want to put all your content in ERB views, then a very simple approach is to create a PagesControllerwhose purpose is to deal with static pages. Each page is represented by one action in the controller.
如果你只是想把你的所有内容都放在 ERB 视图中,那么一个非常简单的方法是创建一个PagesController其目的是处理静态页面的。每个页面由控制器中的一个动作表示。
pages_controller.rb:
pages_controller.rb:
class PagesController < ApplicationController
def home
end
def about
end
def contact
end
end
routes.rb:
路线.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.
然后在 app/views/pages 下创建 home.html.erb、about.html.erb 和 contact.html.erb 视图。这些视图包含您想要在静态页面上显示的任何内容。默认情况下,它们将使用您的应用程序的 application.html.erb 布局。
You'll also want to look into page cachingto give yourself a boost in performance.
您还需要研究页面缓存以提高性能。
Approach #2 - store content in database
方法#2 - 在数据库中存储内容
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 gemto 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.
我使用的另一种方法是为静态页面制作一个非常基本的 CMS。在这种情况下,页面在模型中表示。它使用friendly_id gem来处理每个页面的slug,以便可以通过URL 中的漂亮名称(例如/about)而不是ID 来检索它们。
page.rb:
页面.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:
路线.rb:
match '/:id' => 'pages#show'
Note: put this entry at the end of routes.rb since it matches everything.
注意:将此条目放在 routes.rb 的末尾,因为它匹配所有内容。
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.
然后你想如何创建、编辑和更新页面取决于你 - 你可以有一个管理界面,或者以某种方式将它构建到你的公共界面中。这种方法也可以从页面缓存中受益。
回答by Max Masnick
Another option is the high_voltagegem: https://github.com/thoughtbot/high_voltage
另一种选择是high_voltagegem:https: //github.com/thoughtbot/high_voltage
This makes it super easy to create static pages where the content is stored in views.
这使得创建内容存储在视图中的静态页面变得非常容易。
回答by Richard Jones
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 controllermacroin your routes.
Jeff 的方法 #1(在视图中存储内容并为每个静态页面设置路由和控制器操作)是一个很好的方法。我唯一要添加的是在您的路线中使用controller宏。
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 gethttp 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.
它还使用gethttp 动词方法而不是match,这是 Rails 路由的更好实践(更简洁,现在Rails 4 要求在使用match.
回答by traday
Check out Michael Hartl's http://railstutorial.orgwhich 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.
查看 Michael Hartl 的http://railstutorial.org,它有 2.3.8 和 3.0.x 版本。它通过很好的示例涵盖了这一点,并引导您尽早构建它们,您还将有机会学到比这个示例更多的东西。我强烈推荐它。
回答by timeon
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.
Jeff 的方法 #1 对我很有用。这是一个让控制器动态查找页面的技巧。有了这个,您不需要触摸控制器或 routes.rb 来添加页面。只需将页面放在 app/views/pages 下,控制器就会找到它。
class PagesController < ApplicationController
def show
render params[:id]
end
end
回答by Owen
config/routes.rb
配置/路由.rb
get ':id', to: 'pages#show'
app/controllers/pages_controller.rb
应用程序/控制器/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.)
然后将您的静态页面放在 app/views/pages/{name}.html.erb (或任何模板格式。)
回答by Sumit Munot
For more you can create static pages using Jekyll bootstrapor also Jekyll using Danger blog
如需更多信息,您可以使用Jekyll bootstrap创建静态页面,也可以使用 Danger 博客创建Jekyll
Refer it is very helpful.
参考它非常有帮助。
回答by Ahmed Fathy
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.
我建议将您的页面添加到公共文件夹中,以便直接提供服务,而无需通过 Rails。我不是专家,所以我不确定如果页面是静态的,这是否会有任何缺点。
回答by Dave Sims
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.
对您的问题的充分回答基本上类似于对 Rails 框架的介绍:至少是 MVC 结构、模板和路由 DSL。Jeff 给出了一个很好的尝试,但他的回答仍然假设你有很多基本的 Rails 知识。
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.
不过,我建议,如果您的 web 应用程序真的那么简单,Rails 可能会大材小用。我会研究一些更轻松的东西,比如Sinatra,它的学习曲线比 Rails 低得多,并且在这种事情上做得很好,而无需处理复杂的路由、魔术 MVC 动作/模板映射等。

