-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MissingTemplate issues in Rails 3.0 #4127
Comments
I think we somehow need to
Another thing, I found adding a |
A good suggestion from the previous thread was to introduce the concept of a default mime type to avoid raising missing template errors. In the instances I observed, the requests that weren't specifically requesting JSON or XML would be properly satisfied by behaving as if they requested HTML. I think it would be reasonable to have a default to HTML, and provide a hook to override/remove the default. |
@softwaregravy The "default mime type" only could be used in Configuration, because, "HTML" doesn't meet all the needs of us. like API project, people request "text/*", default to html, will raise error also. because all we provide in that project is json or xml. If it's code in Rails, it should be intelligent a little bit. :) |
I think someone did "implement code that handles text/, appplication/, and image/*" in commit Above 3.2, Mime::Type.parse("text/*") give correct list. |
ok, I think we've been involved into a wrong direction. It's NOT about "rendering correctly for the mime type of the text/*". For example: http://localhost/books.json <= we only provide html template, but didn't write |
This is what I did in my project. it works.
|
Can some one shed some light on that fact the why is this an issue in 3.0.x , how were we handling such issues in 2.x ? May be some one from Core |
This could be a solution to fix this problem in Rails 3.0.x
|
Using exception_notification and rails 3.2 I get tons of "ActionView::MissingTemplate"-mails from various odd user agents like: HTTP_USER_AGENT : Mozilla/4.0 (PSP (PlayStation Portable); 2.00) HTTP_ACCEPT : /;q=0.01HTTP_USER_AGENT : Mozilla/4.0 (MobilePhone SCP-3800/US/1.0) NetFront/3.4 MMP/2.0 HTTP_ACCEPT : application/vnd.wap.xhtml+xml, application/x-pmd, application/vnd.phonecom.mmc-xml, audio/midi, audio/vnd.qcelp, application/xhtml+xml; profile="http://www.wapforum.org/xhtml", multipart/vnd.sprint-pre-cache, multipart/mixed; q=0.5, multipart/related,text/vnd.wap.wml, text/vnd.sun.j2me.app-descriptor, text/x-pcs-gcd, text/css, application/ecmascript, text/ecmascript, image/png, image/gif, image/jpeg; q=0.5, image/vnd.wap.wbmp; q=0.2, audio/qcelp, application/pmd, audio/mid, text/plainHTTP_USER_AGENT : Motorola-V3m-Red Obigo/Q04C1 MMP/2.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 HTTP_ACCEPT : application/xhtml+xml; profile="http://www.wapforum.org/xhtml", application/vnd.wap.xhtml+xml, image/png, image/gif, image/jpeg; q=0.5, image/x-bmp; q=0.3, image/vnd.wap.wbmp; q=0.2, audio/midi, audio/vnd.qcelp, audio/qcelp, audio/aac, audio/aacPlus, audio/amr, audio/mp3, audio/mpeg, audio/imy, audio/imelody, audio/mp4, audio/mp4a-latm, audio/3gpp, audio/3gpp2, video/mp4, video/mp4v-es, video/3gpp, video/3gpp2, application/x-pcs-mcd+xml, application/sdp, application/x-pmd, application/x-cmx, application/pmd, multipart/mixed; q=.1, application/vnd.wap.wmlscriptc, application/vnd.wap.wmlc, text/vnd.wap.wml, text/css, text/vnd.sun.j2me.app-descriptor, text/x-pcs-gcd, text/plain, text/ecmascript, application/ecmascript, multipart/vnd.sprint-pre-cacheTrying the "request.format = :html" sollution now. |
@ippa I think this is not a correct solution. The correct way to do it is to add "respond_to :html" for the actions. as josevalim said. (P.S. although I think adding respond_to for all actions is pretty awkward.) |
@hlxwell: okey... the exception-mail-spam stopped anyhow, and the pages still render fine in all browsers I can test. I'll test your / josevalims suggestion as well. |
false positive, "request.format = :html" didn't stop the mails. Trying "respond_to" now. |
Having a similar issue in rails 3.2.1 when a client sends an accept header like Mime::Type.lookup(accept_header[/^[^;]*/]) The |
Look at this, the latest code on master has this to convert / to correct mime. |
@hlxwell I was not talking about |
Just to get this into the search engines... An accept header like the following, while valid, will also trigger the MissingTemplate error. Specifically, the "; profile=..." bit. The spec says this is valid, but Rails does not like it at all. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html curl -Iv -H 'Accept: application/xhtml+xml; profile="http://www.wapforum.org/xhtml", application/vnd.wap.xhtml+xml' (edit) Digging into it further, it appears Rails doesn't like anything other than 'q' as an extension to the mime type. |
Based on rails#4918. Related to rails#4127.
Hey guys. Could you confirm if this issue was fixed in the 3.2? Thanks. |
I'm facing this issue with Rails 3.0.12 and found no fix. I looked at this StackOverflow question, but it wasn't solving the problem correctly. So I've investigated the issue and implemented a solution relying on a Rack middleware which "corrects" the You can find the middleware, an integration test and an initializer in this gist. Enjoy! |
I'm closing this issue since seems that it was fixed in |
I still have this problem in 3.2.1
I tried to add Any ideas? |
I have this issue in 3.2.8: |
@antage can you give us a way to reproduce? If so, I'll re-open. |
@steveklabnik It's simple, just send a request with curl:
or
|
Based on rails#4918. Related to rails#4127. Conflicts: actionpack/test/dispatch/mime_type_test.rb
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
This is still an issue. Example using rails 4.1.8.
In browser go to localhost:3001/users.gif Rails will throw following:
Expected: Would like to see rails return an empty response with a 406 status if format is not supported. |
Confirmed still occurs in 4.2.0. |
Also I am getting errors with the following |
Confirmed still occurs in Rails 3.2.13. Raising ActionView::MissingTemplate even with a |
We've ran into this issue with Rails 4.1.11: when a user sets the Accept header to "/;" we get a Adding Edit: This seems to be a better solution, in a controller before_filter :set_format_to_html
def set_format_to_html
request.format = 'html'
end |
Works great @gregkare |
That workaround may result in accepting requests which aren't able to properly accept an HTML response, @gregkare, @blarralde. Works if you don't care about this condition (which you likely don't), but if the thing on the other end wants JSON or somesuch, and if you wanted to produce JSON, that before_filter might cost you some debugging time until someone finds and removes it. |
@purp Just found that out. I've added This is what my code looks like: DEFAULT_RESPONSE_FORMAT = :html
before_filter :set_default_response_format
def set_default_response_format
request.format = DEFAULT_RESPONSE_FORMAT if request.format.to_sym.nil?
end |
Hmmm. Don't know, @blarralde. It's been a couple of years since I was deep in that code. However, @antage's noted (Oct 19, 2012) that "hc/url;/" tripped it, which I think wouldn't show nil. Depends on how request.format maps to the Accept header. In the end, if it works okay for you, you're probably good. Man, we need to get this one fixed. |
I've tested it multiple bogus accept headers, and since they're not in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_types.rb they don't have a symbol set. When a format is passed to the URL or as a accept header it takes precedence over the default response format. So:
|
My previous solution created bugs in some parts of the app where format might be implied. So eventually I've replaced it with: BOGUS_REQUEST_FORMATS = ['*/*;', '/;', 'hc/url;*/*']
DEFAULT_RESPONSE_FORMAT = :html
before_filter :set_default_response_format
def set_default_response_format
request.format = DEFAULT_RESPONSE_FORMAT if request.format.in? BOGUS_REQUEST_FORMATS
end |
headers on the /rss route. There must be a better way... see: - http://stackoverflow.com/questions/23946630/rails-4-how-to-render-json-regardless-of-requested-format - rails/rails#4127
I was experiencing this issue with bots (and could reproduce with a curl command). JSON was my default format, which was wrong. I noticed I had this in my application_controller.rb respond_to :json, :html Changed it to respond_to :html, :json And the issue is resolved. |
I have the same issue. ActionView::MissingTemplate: Missing template /home/index with {:formats=>[:json, :js, "/"] |
Having this issue in 4.2.3 Mostly from bots |
I confirmed this issue in Rails 4.2 with:
I found this issue was caused by a browser sending a invalid Http Accept header, which is This happens rarely unless you're in China and programmers in Baidu.com made a fucking silly mistake. And most importantly, they have a large number of users using the buggy browser they made. Can't Rails framework just ignore this and set to the default format :html instead of throwing a exception? @dhh |
Sadly, @ryancheung, that leads to unintended consequences as a few folks in this thread have discovered. Accepting See this comment above and the next seven or so comments for an illustration of where it goes wrong. |
THIS IS FINALLY FIXED IN RAILS 5.0.0! The example to reproduce this below will properly return Here's a quick and easy way to reproduce with any Rails version prior to 5.0.0:
If it's broken, you'll get this in the curl terminal:
... and something like this in the server terminal:
In Rails 5.0.0 you get this in the curl terminal:
... and something like this in the server terminal:
If you still need a workaround, here's what a slightly wiser me would offer now: class ApplicationController < ActionController::Base
rescue_from ActionView::MissingTemplate, :with => :catch_unacceptable_requests
def catch_unacceptable_requests(exception)
if !ActionController::MimeResponds::Collector.new(collect_mimes_from_class_level).negotiate_format(request)
head :not_acceptable
else
raise exception
end
end (about friggin' time! ;) |
@purp Thanks! This should only be worked around in my own site. For illegal accept headers, either fallback to :html or return 406. |
@purp I am using Rails 5 and still see "missing a template for this request format and variant". If I previously upgraded from 4 to 5, what are the things I should remove manually to fix this? Thanks! |
@h8rry I'm unclear what you're asking here. Are you saying that you implemented the fix above in Rails 4, and upon upgrading to Rails 5 you're having this problem, or is this problem with a new Rails 5 app? |
This issue was reported earlier, but its not fixed in 3.0.x latest and some people have reported in 3.1.x as well.
#701
The text was updated successfully, but these errors were encountered: