ActionView :: TemplateError (missing a template) In ruby ​​on rails

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

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 in the working environment, but had no luck, for some reason the rails application asks for folder/_file_name.erb in some cases, and in other cases it searches for the correct file folder/_file_name.html.erb .

Can someone explain to me what is going on?

The same thing happens with .rhtml files, rails application.erb applications, while others get the correct .rhtml file

update:

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

Any pointers to this problem would be helpful, thanks in advance

+6
ruby ruby-on-rails rendering actionview
source share
3 answers

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

+4
source share

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

If you have a foo controller with an action in the bar and a response defined in your action, Rails will try to process view / foo / bar.html.erb.

If this is not what you want, you need to define the answer in your controller and tell Rails to display the relevant particles, for example:

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

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

+5
source share

In some cases, you cannot prevent this error, because there are reasons for loading, such as lack of cache, unknown request format, etc.

You can try to limit the number of predefined formats, for example:

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

However, I added the following method to the application_controller.rb file so that such errors display a 404 error page with an error message on the screen

 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 

can you wrap this code in an if statement, something like this if Rails.env.production? given that env is set up so that your development environment is not affected

+3
source share

All Articles