Rails 4 - MissingTemplate/Missing Partial Error, Only in Production - ruby-on-rails

We just secured our application with an SSL certificate. Moving to HTTPS is proved more complex than we thought and we're currently sorting through a few of the resulting bugs.
We have an AJAX call in CoffeeScript, where rails responds by rendering the html of a partial. This is working perfectly in development.
CoffeeScript:
coffee_method: (pos, uid) =>
$.ajax '/contoller/test/',
type: 'POST'
data:
pos: pos
uid: uid
success: (data) ->
$('#result-div').html(data) #Populates side menu with _next_destination_menu content
error: ->
alert 'How embarassing! Something went wrong - please try your search again. '
Controller:
def test
... #do some stuff
puts "format requested: #{request.format}"
puts "format url_encodeded: #{request.format.url_encoded_form?}"
render partial: 'trips/_test' #app/views/trips/_test.html.erb
end
However, in production, we get the following error:
ActionView::MissingTemplate (Missing partial trips/_test with {:locale=>[:en], :formats=>[:url_encoded_form], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
After some digging, I figured out the request in production was a different format. Using those puts lines in the controller to debug, here are the results:
Production:
format requested: application/x-www-form-urlencoded
format url_encodeded: true
Development:
format requested: */*
format url_encodeded: false
What is the best way to handle this issue. Do I:
Change the content type of every AJAX call in the CoffeeScript?
Add a respond_to...format.url_encoded_form {render partial: trips/test} to the controller?
The latter seems like I would be duplicating code, because I want to render the same partial, regardless of the format the request comes in. I tried format.all {...} but encountered the same issue. Any best practices are appreciated!
Update:
Specifying the response type directly gives me the same missing template error:
respond_to do |format|
format.html {render partial: 'trips/test'}
format.json {render partial: 'trips/test' }
format.url_encoded_form {render partial: 'trips/test'}
end
Update 2:
The Request headers for both localhost and production are the same, the content type is application/x-www-form-urlencoded, even though format.url_encoded_form? returns false.
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Connection:keep-alive
Content-Length:37
Content-Type:application/x-www-form-urlencoded; charset=UTF-8

You could try passing in the format, locale, and handlers exactly as requested:
render(
   partial: 'trips/test',
     formats: [:html, :js, :json, :url_encoded_form],
     locale: [:en],
     handlers: [:erb, :builder, :raw, :ruby, :coffee, :jbuilder])

Related

Missing template error for Ruby on Rails Application

I have a view in the LoginController with the following code
def home
render 'home'
end
However, whenever I load my web server, it shows the error message:
ActionView::MissingTemplate (Missing template login/home, application/home with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Users/60984/prairielearn-eecs/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actiontext-6.1.7.2/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actionmailbox-6.1.7.2/app/views"
):
app/controllers/login_controller.rb:16:in `home'
Even though I have the file in the directory app/views/login/home.html.erb.
I tried changing it to a more specific directory such as
def home
render 'login/home'
end
but it still fails. However, I tried using the whole path like
render file: 'C:/Users/.../app/views/login/home.html.erb'
With the code above, the problem changed into the html.erb is not being interpreted, it will display the code blocks on the browser no matter what browser I use.
I am just confused about why it doesn’t work since I have the html file in the designated place.

Problem with sitemap.xml in Ruby on Rails app

I have Ruby on Rails app and I created sitemap.xml file (in public directory) with sitemap_generator gem but it can't be readable in production.
sitemap.rb
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://example.com"
SitemapGenerator::Sitemap.namer = SitemapGenerator::SimpleNamer.new(:sitemap, :extension => '.xml')
SitemapGenerator::Sitemap.create do
Category.find_each do |cat|
add category_path(cat), priority: 0.8, changefreq: 'daily', lastmod: cat.updated_at
end
Job.where(status: true).find_each do |job|
add job_path(job), priority: 0.6, lastmod: job.updated_at
end
end
errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render status: 404
end
def unprocessable_entity
render status: 422
end
def internal_server_error
render status: 500
end
end
I have all views for each kind of errors but Heroku logs show me:
I, [2019-10-16T11:57:53 #4] INFO -- : Processing by ErrorsController#not_found as XML
I, [2019-10-16T11:57:53 #4] INFO -- : Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
2019-10-16T11:57:53 app[web.1]: Error during failsafe response: Missing template errors/not_found, application/not_found with {:locale=>[:en], :formats=>[:xml], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder, :haml]}. Searched in:
2019-10-16T11:57:53 app[web.1]: * "/app/app/views"
The command heroku run rails sitemap:refresh:no_ping is creating all links that I want but when I try to see the link https://mywebsite.com/sitemap.xml - I get 500 error and I can't see the content - I don't know why. I think it can be some problems with error controller because https://mywebsite.com/sitemap (without .xml) displays 404 page. With .xml it is 500 error.
On localhost everything works fine but in production mode on Heroku I have problems.
As I know, sitemap_generator creates sitemap.xml.gz file. Try open your-site.com/sitemap.xml.gz. Works on my project.

Missing partial when using curl for ComfortableMexicanSofa page

I've created a partial for use in my CMS page at:
/app/views/users/_register_home_page.html.haml
My CMS page looks like this:
{{ cms:partial:/users/register_home_page }}
When I call this from the browser (ie, Chrome) it works fine. Also when I go incognito (to ensure there's no cookies being sent)
calling my test page from curl:
curl "http://localhost:5000/test"
returns
ActionView::MissingTemplate at /
================================
> Missing partial users/_register_home_page with {:locale=>[:en], :formats=>[:all], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}. Searched in:
* "/Users/doug/dev/my-rails-app/app/views"
actionview (4.1.7) lib/action_view/path_set.rb, line 46
-------------------------------------------------------
41 end
42 METHOD
43 end
44
45 def find(*args)
> 46 find_all(*args).first || raise(MissingTemplate.new(self, *args))
47 end
48
49 def find_all(path, prefixes = [], *args)
50 prefixes = [prefixes] if String === prefixes
51 prefixes.each do |prefix|
I'd expect curl to return the same results.
Update: The error occurs when Accept: text/html is ommitted from the curl statement.
My solution for now was to modify the CMS gem to explicitly use :formats => [:html] in the partial tag, and then specify the full path (including html.haml) of the partial like so:
{{ cms:partial:/users/register_home_page.html.haml }}
A branch where this fix is here:
https://github.com/mvcodeclub/comfortable-mexican-sofa
I've also filed a Github issue

Strange MissingTemplate exception with :formats=>[:jpeg, "image/pjpeg", :png, :gif]

In production, we regularly get the following exception:
An ActionView::MissingTemplate occurred in constructions#show:
Missing template constructions/show, application/show with {:locale=3D>[:=
ru], :formats=3D>[:jpeg, "image/pjpeg", :png, :gif], :handlers=3D>[:erb, :b=
uilder, :coffee, :jbuilder, :haml]}
What puzzles me here is the formats hash, which requests for some image (:jpeg, "image/pjpeg", :png, :gif). We have no custom MIME types registered in our app, and as far as I know there's no corresponding Rails default MIME-type.
So the question is: what kind of request generates this formats hash?
I got same error as well. I notices this is from a search engine of "YandexImage" trying to get custom format. On my controller and action is just empty, because it is a static *.html.erb page. Here is more information.
* DOCUMENT_ROOT : /srv/www/apps/mysite/current/public
* HTTP_ACCEPT : image/jpeg, image/pjpeg, image/png, image/gif
* HTTP_CONNECTION : Keep-Alive
* HTTP_FROM : support#search.yandex.ru
* HTTP_HOST : mysite.com
* HTTP_USER_AGENT : Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)
* ORIGINAL_FULLPATH : /
Two ways to fix this:
Edit public/robots.txt to block YandexImage. see more information at http://yandex.com/bots
User-agent: YandexImage
Disallow: /
Or put following code to your action, it will handle only html otherwise raise the not found page
respond_to do |format|
format.html
format.any { raise ActionController::RoutingError.new('Not Found') }
end

ActionView::MissingTemplate: Rails not looking for JSON format

I'm using Backbone.js and thus bootstrapping data using a JSON (jbuilder) partial like so (using HAML):
App.users = new App.UserList(#{render('users/index', :formats => [:json], :handlers => [:jbuilder], locals: {users: #users})})}, {silent:true});
It gives me this error:
ActionView::Template::Error (Missing partial users/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder, :arb, :coffee, :haml]}. Searched in:
Notice that it is only looking for the ':html' format, despite me passing in 'formats: [:json]'. Should I be doing something differently?
Thank you for any help.
What version of Rails are you using? This problem was fixed for 3.2.3, but exists in earlier versions.
For a quick fix, though it will cause deprecation warnings in Rails 3.2 and later, you can add the format to the name of the template, i.e. render('users/index.json' ...

Resources