ActionView::TemplateError(缺少模板)在 ruby​​ on rails 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5102897/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:29:59  来源:igfitidea点击:

ActionView::TemplateError (Missing template) In ruby on rails

ruby-on-railsrubyrenderingactionview

提问by wael34218

I am running a RoR application (rails 2.3.8, ruby 1.8.7), the application runs fine on my local machine. but on production the logs show the following error:

我正在运行一个 RoR 应用程序(rails 2.3.8,ruby 1.8.7),该应用程序在我的本地机器上运行良好。但在生产中,日志显示以下错误:

ActionView::TemplateError (Missing template folder/_file_name.erb in view path app/views) on line #19 of app/views/layouts/main.rhtml:
19:     <%= render :partial => "folder/file_name" -%>

the file name exists as folder/_file_name.html.erb, I tried to reproduce the problem on the production environment but didnt have any luck, for some reason rails application asks for folder/_file_name.erbat some times while other times it searches for the right file folder/_file_name.html.erb.

文件名存在为folder/_file_name.html.erb,我试图在生产环境中重现该问题,但没有任何运气,出于某种原因,rails 应用程序folder/_file_name.erb有时会要求,而有时它会搜索正确的文件folder/_file_name.html.erb

Could someone explain to me what is going on?

有人可以向我解释发生了什么吗?

The same also occurs for .rhtml files, rails application requests .erb at times while others get the right .rhtml file

.rhtml 文件也会发生同样的情况,rails 应用程序有时会请求 .erb,而其他人会得到正确的 .rhtml 文件

update:

更新:

<%= render :partial => "shared/meta_tags" -%>
<%= render :partial => "shared/common_resources" -%>
<%= render :partial => 'shared/ads/oas' -%>

Any pointers on this issue will be helpful, thanks in advance

关于此问题的任何指示都会有所帮助,提前致谢

采纳答案by Fernando Diaz Garrido

Whats the format of the request?, for the first template (folder/_file_name.html.erb) it will only be correct if the request format is html, but not if it is ajax or any other custom type you have in your app. One quick soluction would be to rename it to folder/_file_name.erbif you want to use the same partial for all the formats

请求的格式是什么?,对于第一个模板 ( folder/_file_name.html.erb),只有当请求格式是 html 时才正确,但如果它是 ajax 或您的应用程序中的任何其他自定义类型,则不正确。folder/_file_name.erb如果您想对所有格式使用相同的部分,一个快速的解决方案是将其重命名为

回答by Jeff Keen

Is there a controller action with the same name as that file?

是否有与该文件同名的控制器操作?

If you have a foo controller with a bar action and no response defined in your action, Rails will try and render view/foo/bar.html.erb.

如果你有一个带有 bar 动作的 foo 控制器并且你的动作中没有定义响应,Rails 将尝试渲染 view/foo/bar.html.erb。

If that's not what you want, you need to define a response in your controller and tell Rails to render the appropriate partial, like so:

如果这不是您想要的,您需要在控制器中定义一个响应并告诉 Rails 呈现适当的部分,如下所示:

respond_to do |format|
   format.html do
       render :partial => "/foo/bar"
   end
end

In the latter case, Rails will render "views/foo/_bar.html.erb"

在后一种情况下,Rails 将呈现“views/foo/_bar.html.erb”

回答by shakirthow

In some cases You can't prevent this error as there are load reasons like missing cache, unknown request format and etc

在某些情况下,您无法阻止此错误,因为存在加载原因,例如缺少缓存、未知请求格式等

You can try to restrict the number of predefined formats like:

您可以尝试限制预定义格式的数量,例如:

get '/about-us' => 'controller#about', :format => /(?:|html|json)/

However, I added the following method in my application_controller.rb file so that such errors will render a 404 page rather failing with a error message on screen

但是,我在 application_controller.rb 文件中添加了以下方法,以便此类错误将呈现 404 页面而不是失败并在屏幕上显示错误消息

rescue_from ActionView::MissingTemplate, :with => :rescue_not_found
protected
def rescue_not_found
  Rails.logger.warn "Redirect to 404, Error: ActionView::MissingTemplate"
  redirect_to '/404' #or your 404 page
end

you can wrap this code in a if statement, something like this if Rails.env.production?given that the env is setup so your dev environment wont be affected

您可以将此代码包装在 if 语句中,if Rails.env.production?鉴于已设置 env,因此您的开发环境不会受到影响,就像这样