Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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.erb at some times while other times it searches for the right file 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

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

like image 981
wael34218 Avatar asked Feb 24 '11 09:02

wael34218


3 Answers

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.erb if you want to use the same partial for all the formats

like image 146
Fernando Diaz Garrido Avatar answered Oct 21 '22 22:10

Fernando Diaz Garrido


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 views/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:

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"

like image 29
Jeff Keen Avatar answered Oct 21 '22 22:10

Jeff Keen


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

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

like image 20
shakirthow Avatar answered Oct 21 '22 21:10

shakirthow