Ruby-on-rails 如何在 Rails 中做静态内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1146624/
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
How to do static content in Rails?
提问by Timothy T.
Looking at different options:
查看不同的选项:
One is to just put the static pages in the public/ folder, but I do want the header from layout/application to be consistent.
一种是将静态页面放在 public/ 文件夹中,但我确实希望布局/应用程序的标题保持一致。
I tried this, but I got an error:
我试过这个,但出现错误:
# in routes.rb:
map.connect '*path', :controller => 'content', :action => 'show'
# in content_controller.rb:
def show
render :action => params[:path].join('/')
end
All I want is an easy way to put together things like my faq, contact, tos, privacy, and other non-application type pages somewhere easy by just creating an .rhtml. who has done this?
我想要的只是一种简单的方法,只需创建一个 .rhtml,就可以轻松地将我的常见问题解答、联系方式、tos、隐私和其他非应用程序类型页面放在一起。谁做过这件事?
采纳答案by Gabe Martin-Dempesy
thoughtbot has a plugin called high_voltage for displaying static content: https://github.com/thoughtbot/high_voltage
Thoughtbot 有一个名为 high_voltage 的插件用于显示静态内容:https: //github.com/thoughtbot/high_voltage
回答by Roland Studer
For Rails5and Rails4you can do the following:
对于Rails5和Rails4,您可以执行以下操作:
Put the line below at the end of your routes.rb
将下面的行放在 routes.rb 的末尾
get ':action' => 'static#:action'
Then requests to root/welcome, will render the /app/views/static/welcome.html.erb.
然后对root/welcome 的请求将呈现/app/views/static/welcome.html.erb。
Don't forget to create a 'static' controller, even though you don't have to put anything in there.
不要忘记创建一个“静态”控制器,即使您不必在其中放置任何东西。
For Rails3you have to use 'match' instead of 'get'
对于Rails3,您必须使用“匹配”而不是“获取”
match ':action' => 'static#:action'
回答by Omar Qureshi
depends on the url structure, if you want the paths to come off of / (e.g. /about_us), then:
取决于 url 结构,如果您希望路径脱离 /(例如 /about_us),则:
map.connect ':action', :controller => "static"
This should go at the very end of your routes file, Throw your .html.erb files into app/views/static and you are done.
这应该放在你的路由文件的最后,将你的 .html.erb 文件放入 app/views/static 中,你就完成了。
e.g: throwing in about_us.html.erb, will give you a page at /about_us.
例如:扔进去about_us.html.erb,会给你一个/about_us 的页面。
The item that you have in your question is great for a catch all route where you can analyze the array given to you at params[:path]. A bit more information on that at http://railscasts.com/episodes/46-catch-all-route
您在问题中的项目非常适合捕获所有路线,您可以在其中分析在params[:path]. 有关更多信息,请访问http://railscasts.com/episodes/46-catch-all-route
回答by Tate Johnson
Rendering an action doesn't make sense. You'll want to render a template (or a file) with a layout.
渲染动作没有意义。您需要使用布局呈现模板(或文件)。
# Path relative to app/views with controller's layout
render :template => params[:path]
# ... OR
# Absolute path. You need to be explicit about rendering with a layout
render :file => params[:path], :layout => true
You could serve a variety of different templates from a single action with page caching.
您可以使用页面缓存从单个操作提供各种不同的模板。
# app/controllers/static_controller.rb
class StaticController < ApplicationController
layout 'static'
caches_page :show
def show
valid = %w(static1 static2 static3)
if valid.include?(params[:path])
render :template => File.join('static', params[:path])
else
render :file => File.join(Rails.root, 'public', '404.html'),
:status => 404
end
end
end
Lastly, we'll need to define a route.
最后,我们需要定义一个路由。
# config/routes.rb
map.connect 'static/:path', :controller => 'static', :action => 'show'
Try accessing these static pages. If the path doesn't include a valid template, we'll render the 404 file and return a 404 status.
尝试访问这些静态页面。如果路径不包含有效模板,我们将呈现 404 文件并返回 404 状态。
http://localhost:3000/static/static1http://localhost:3000/static/static3http://localhost:3000/static/static2
http://localhost:3000/static/static1http://localhost:3000/static/static3http://localhost:3000/static/static2
If you take a look in app/public you'll notice a static/ directory with static1.html, static2.html and static3.html. After accessing the page for the first time, any subsequent requests will be entirely static thanks to page caching.
如果您查看 app/public,您会注意到一个包含 static1.html、static2.html 和 static3.html 的 static/ 目录。第一次访问页面后,由于页面缓存,任何后续请求都将是完全静态的。
回答by gkrdvl
Considering if u have 1 Home Controller with couple method like show, aboutus, privacy :
考虑到您是否有 1 个家庭控制器,其中包含 show、aboutus、privacy 等几种方法:
class HomesController < ApplicationController
def show
end
def privacy
end
def aboutus
end
end
And map the show method to your root, and map the other to some named routes like
并将 show 方法映射到您的根,并将另一个映射到一些命名路由,例如
map.root :controller => "homes", :action => "show"
map.aboutus "/aboutus", :controller => "homes", :action => "aboutus"
map.privacy "/privacy", :controller => "homes", :action => "privacy"
And with view for each
并为每个
app/views/homes/aboutus.html.erb --> you get http://localhost:3000/aboutus
app/views/homes/show.html.erb --> you get http://localhost:3000 (root)
app/views/homes/privacy.html.erb --> you get http://localhost:3000/privacy
All using the same layout at app/views/layout/application.html.erb
所有在 app/views/layout/application.html.erb 使用相同的布局
回答by ecleel
回答by GoodViber
Create a PagesController for your static pages (e.g contact) and insert
为您的静态页面(例如联系人)创建一个 PagesController 并插入
def contact_page
end
in config/routes.rb insert
在 config/routes.rb 中插入
get 'contact' => 'pages#contact_page'
which will display the content from views/pages/contact_page.html.erb
这将显示来自 views/pages/contact_page.html.erb 的内容
回答by eriese
I used the idea of a generalized controller from the answers given, but I wanted to catch 404s, so I put an action in it to handle either case:
我从给出的答案中使用了通用控制器的想法,但我想捕捉 404,所以我在其中放置了一个动作来处理任何一种情况:
# app/controllers/static_controller.rb
class StaticController < ApplicationController
def static_or_404
render params[:static]
rescue ActionView::MissingTemplate
render :not_found
end
end
and then at the very bottom in my routing:
然后在我的路由的最底部:
# config/routes.rb
get '*static', to: 'static#static_or_404'
It serves the view from app/views/staticof the same name as the path, and if there isn't such a view, it serves app/views/static/not_found.html.erb. One could also replace render :not_foundwith redirect_to root_pathor anything else one wanted to have happen.
它提供与app/views/static路径同名的视图,如果没有这样的视图,则提供app/views/static/not_found.html.erb. 也可以替换render :not_found为redirect_to root_path或任何其他想要发生的事情。

