Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中执行静态内容?_Ruby On Rails_Ruby_Static Content - Fatal编程技术网

Ruby on rails 如何在Rails中执行静态内容?

Ruby on rails 如何在Rails中执行静态内容?,ruby-on-rails,ruby,static-content,Ruby On Rails,Ruby,Static Content,查看不同的选项: 一种是将静态页面放在public/文件夹中,但我确实希望布局/应用程序的标题保持一致 我尝试了这个,但我得到了一个错误: # in routes.rb: map.connect '*path', :controller => 'content', :action => 'show' # in content_controller.rb: def show render :action => params[:path].join('/') end 我想要

查看不同的选项:

一种是将静态页面放在public/文件夹中,但我确实希望布局/应用程序的标题保持一致

我尝试了这个,但我得到了一个错误:

# in routes.rb:
map.connect '*path', :controller => 'content', :action => 'show'

# in content_controller.rb:
def show
  render :action => params[:path].join('/')
end

我想要的只是一种简单的方式,通过创建一个.rhtml,将我的faq、contact、tos、privacy和其他非应用程序类型的页面放在一起。谁做了这件事?

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

# 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
尝试访问这些静态页面。如果路径不包含有效模板,我们将呈现404文件并返回404状态

  • http://localhost:3000/static/static1
  • http://localhost:3000/static/static3
  • http://localhost:3000/static/static2

如果您在app/public中查看,您会注意到一个包含static1.html、static2.html和static3.html的static/目录。第一次访问页面后,由于页面缓存,任何后续请求都将是完全静态的。

考虑到您是否有一个主控制器,具有show、aboutus、privacy等耦合方法:

# 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
class HomesController < ApplicationController
  def show
  end
  def privacy
  end
  def aboutus
  end
end
并为每个

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/layout/application.html.erb使用相同布局取决于url结构,如果您希望路径来自/(例如/关于我们),则:

这应该放在routes文件的最末尾,将.html.erb文件放入app/views/static,就完成了

e、 g:加入
about_-us.html.erb
,你会看到一个关于我们的页面


您的问题中的项目非常适合于全面分析路径,您可以在
params[:path]
处分析提供给您的数组。关于这方面的更多信息,thoughtbot有一个名为high_voltage的插件,用于显示静态内容:

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

将下面的线放在路线的末尾。rb

  get ':action' => 'static#:action'
然后对root/welcome的请求将呈现/app/views/static/welcome.html.erb

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

限制:如果有人试图访问一个不存在的页面,它将抛出一个应用程序错误

对于Rails3您必须使用“匹配”而不是“获取”

  match ':action' => 'static#:action'
这是我见过的最好的解决方案之一。他构建了一个缓存当git版本更改时过期的静态页面

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

@页面名称%>

为静态页面(例如联系人)创建页面控制器并插入

def contact_page
end
在config/routes.rb中插入

get 'contact' => 'pages#contact_page'

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

根据给出的答案,我使用了通用控制器的概念,但我想捕获404,因此我在其中添加了一个操作来处理这两种情况:

#app/controllers/static_controller.rb
类StaticController<应用程序控制器
def静态_或_404
渲染参数[:静态]
救援行动视图::丢失模板
渲染:未找到
结束
结束
然后在我的路线的最底层:

#config/routes.rb
将“*static”改为:“static#static _或_404”

它提供与路径同名的
app/views/static
视图,如果没有这样的视图,则提供
app/views/static/not_found.html.erb
。你也可以用
重定向到根路径
或任何其他你希望发生的事情来替换
渲染:找不到

+1真棒,正是我需要的。这应该会得到更多的选票,因为这是最新的Rails;如果您将其命名为“:id”或“:title”或其他任何名称,它将不起作用。在Rails4中,我在开发模式中收到一条警告,要求将“匹配”替换为“获取”。@RolandStuder抱歉,我不清楚-您所要做的就是将“匹配”替换为“获取”,它可以完美地工作。:)如何避免异常,因为这是一个包罗万象的问题?理想情况下,您希望给出404,而不是触发异常。我试图找到这是如何记录的,它并没有触发一个丢失的方法,它只是在磁盘上寻找视图。为什么要进行向下投票?我很感激我的答案与Rails 4有关,但我还是会回答一些人的问题。太棒了!我刚刚回到我的答案,想写下如何处理404,然后我看到你已经这样做了,现在我链接到你的解决方案。在某种程度上,更新我的答案以获得可见性仍然是件好事。或者你想对我的答案进行编辑,这样你就可以得到一些归属。非常值得投票。我希望最好的答案不会在最少的选票下停留太久。这很好。我能够拔出高压并用8行代码替换它。非常感谢,这非常有帮助。
def contact_page
end
get 'contact' => 'pages#contact_page'