如何在 Rails 中做静态内容?

发布于 2024-07-29 00:52:35 字数 387 浏览 14 评论 0原文

查看不同的选项:

一种是将静态页面放在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

愛上了 2024-08-05 00:52:35

对于 Rails6Rails5Rails4,您可以执行以下操作:

将以下行放在 paths.rb 的末尾

  get ':action' => 'static#:action'

,然后向 root/welcome,将渲染/app/views/static/welcome.html.erb

不要忘记创建一个“静态”控制器,即使您不必在其中放置任何内容。

限制:如果有人尝试访问不存在的页面,则会抛出应用程序错误。 请参阅下面可以处理 404 的解决方案

对于 Rails3,您必须使用“match”而不是'得到'

  match ':action' => 'static#:action'

For Rails6, Rails5 and Rails4 you can do the following:

Put the line below at the end of your routes.rb

  get ':action' => 'static#:action'

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'

  match ':action' => 'static#:action'
扛刀软妹 2024-08-05 00:52:35

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

丑丑阿 2024-08-05 00:52:35

取决于 url 结构,如果您希望路径离开 / (例如 /about_us),那么:

map.connect ':action', :controller => "static"

这应该位于路由文件的最末尾,将 .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:

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.

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

臻嫒无言 2024-08-05 00:52:35

渲染一个动作没有意义。 您需要渲染带有布局的模板(或文件)。

# 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

您可以使用页面缓存通过单个操作提供各种不同的模板。

# 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

最后,我们需要定义一条路线。

# config/routes.rb
map.connect 'static/:path', :controller => 'static', :action => 'show'

尝试访问这些静态页面。 如果路径不包含有效模板,我们将渲染 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.

# 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.

  • 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.

瀟灑尐姊 2024-08-05 00:52:35

我从给出的答案中使用了通用控制器的想法,但我想捕获 404,因此我在其中放置了一个操作来处​​理这两种情况:

# app/controllers/static_controller.rb
class StaticController < ApplicationController
    def static_or_404
        render params[:static]
    rescue ActionView::MissingTemplate
        render :not_found
    end
end

然后在我的路由的最底部:

# config/routes.rb
get '*static', to: 'static#static_or_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:

# 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/static of 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_found with redirect_to root_path or anything else one wanted to have happen.

篱下浅笙歌 2024-08-05 00:52:35

考虑到您是否有 1 个带有 show、aboutus、privacy 等耦合方法的 Home Controller:

class HomesController < ApplicationController
  def show
  end
  def privacy
  end
  def aboutus
  end
end

并将 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 every

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

全部在 app/views/ 中使用相同的布局布局/application.html.erb

Considering if u have 1 Home Controller with couple method like 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

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

梦在深巷 2024-08-05 00:52:35

Lindsaar 解决方案是我见过的最好的解决方案之一。 他构建了一个缓存静态页面,当 git 版本更改时该静态页面就会过期。

<%= cache "site-page-#{@page_name}-#{App.git_revision}" do %>
  <%= render :partial => @page_name %>
<% end %>

Lindsaar solution is one of the best I ever seen. He build a caching static pages that expired when git revision changed.

<%= cache "site-page-#{@page_name}-#{App.git_revision}" do %>
  <%= render :partial => @page_name %>
<% end %>
终弃我 2024-08-05 00:52:35

为您的静态页面(例如联系人)创建一个 PagesController 并插入

def contact_page
end

config/routes.rb insert 中

get 'contact' => 'pages#contact_page'

,它将显示views/pages/contact_page.html.erb 中的内容

Create a PagesController for your static pages (e.g contact) and insert

def contact_page
end

in config/routes.rb insert

get 'contact' => 'pages#contact_page'

which will display the content from views/pages/contact_page.html.erb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文