如何在 Rails 中做静态内容?
查看不同的选项:
一种是将静态页面放在 public/ 文件夹中,但我确实希望布局/应用程序中的标题保持一致。
我尝试了这个,但出现了错误:
# in routes.rb:
map.connect '*path', :controller => 'content', :action => 'show'
# in content_controller.rb:
def show
render :action => params[:path].join('/')
end
我想要的只是一种简单的方法,只需创建一个 .rhtml,即可将我的常见问题解答、联系方式、服务条款、隐私和其他非应用程序类型页面等内容放在一起。 谁这样做了?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于 Rails6、Rails5 和 Rails4,您可以执行以下操作:
将以下行放在 paths.rb 的末尾
,然后向 root/welcome,将渲染/app/views/static/welcome.html.erb。
不要忘记创建一个“静态”控制器,即使您不必在其中放置任何内容。
限制:如果有人尝试访问不存在的页面,则会抛出应用程序错误。 请参阅下面可以处理 404 的解决方案
对于 Rails3,您必须使用“match”而不是'得到'
For Rails6, Rails5 and Rails4 you can do the following:
Put the line below at the end of your routes.rb
Then requests to root/welcome, will render the /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.
Limitation: If somebody tries to access a page that does not exist, it will throw an application error. See this solution below that can handle 404s
For Rails3 you have to use 'match' instead of 'get'
Thoughtbot 有一个名为 high_Voltage 的插件,用于显示静态内容:https://github.com/thoughtbot/high_Voltage
thoughtbot has a plugin called high_voltage for displaying static content: https://github.com/thoughtbot/high_voltage
取决于 url 结构,如果您希望路径离开 / (例如 /about_us),那么:
这应该位于路由文件的最末尾,将 .html.erb 文件放入 app/views/static 并你完成了。
例如:输入
about_us.html.erb
,将为您提供一个位于 /about_us 的页面。您问题中的项目非常适合捕获所有路径,您可以在其中分析
params[:path]
处提供给您的数组。 有关更多信息,请访问 http://railscasts.com/episodes/46-catch-全路线depends on the url structure, if you want the paths to come off of / (e.g. /about_us), then:
This should go at the very end of your routes file, Throw your .html.erb files into app/views/static and you are done.
e.g: throwing in
about_us.html.erb
, will give you a page at /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渲染一个动作没有意义。 您需要渲染带有布局的模板(或文件)。
您可以使用页面缓存通过单个操作提供各种不同的模板。
最后,我们需要定义一条路线。
尝试访问这些静态页面。 如果路径不包含有效模板,我们将渲染 404 文件并返回 404 状态。
http://localhost:3000/static/static1
http://localhost:3000/static/static3
http://localhost:3000/static/static2
如果您查看 app/public,您会注意到有一个 static/ 目录,其中包含 static1.html、static2.html 和 static3.html。 第一次访问页面后,由于页面缓存,任何后续请求都将完全静态。
Rendering an action doesn't make sense. You'll want to render a template (or a file) with a layout.
You could serve a variety of different templates from a single action with page caching.
Lastly, we'll need to define a route.
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.
http://localhost:3000/static/static1
http://localhost:3000/static/static3
http://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.
我从给出的答案中使用了通用控制器的想法,但我想捕获 404,因此我在其中放置了一个操作来处理这两种情况:
然后在我的路由的最底部:
它提供来自
的视图app/views/static
与路径同名,如果没有这样的视图,则提供app/views/static/not_found.html.erb
。 人们还可以用redirect_to root_path
或任何其他人们想要发生的事情来替换render :not_found
。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:
and then at the very bottom in my routing:
It serves the view from
app/views/static
of the same name as the path, and if there isn't such a view, it servesapp/views/static/not_found.html.erb
. One could also replacerender :not_found
withredirect_to root_path
or anything else one wanted to have happen.考虑到您是否有 1 个带有 show、aboutus、privacy 等耦合方法的 Home Controller:
并将 show 方法映射到您的根目录,并将另一个映射到一些命名路由,例如
And with view for every
全部在 app/views/ 中使用相同的布局布局/application.html.erb
Considering if u have 1 Home Controller with couple method like show, aboutus, privacy :
And map the show method to your root, and map the other to some named routes like
And with view for each
All using the same layout at app/views/layout/application.html.erb
Lindsaar 解决方案是我见过的最好的解决方案之一。 他构建了一个缓存静态页面,当 git 版本更改时该静态页面就会过期。
Lindsaar solution is one of the best I ever seen. He build a caching static pages that expired when git revision changed.
为您的静态页面(例如联系人)创建一个 PagesController 并插入
config/routes.rb insert 中
,它将显示views/pages/contact_page.html.erb 中的内容
Create a PagesController for your static pages (e.g contact) and insert
in config/routes.rb insert
which will display the content from views/pages/contact_page.html.erb