Missing template blogs/index on Ruby on Rails project

前端 未结 6 1309
野的像风
野的像风 2020-12-29 09:11

For one of my projects I\'m getting this exception every now and then:

ActionView::MissingTemplate: Missing template blogs/index with {:handlers=>[:rx

相关标签:
6条回答
  • 2020-12-29 09:50

    Add formats: [:html] to render:

    def action
      render formats: [:html]
    end
    
    0 讨论(0)
  • 2020-12-29 09:59

    I fixed this issue (a few minutes ago – so far, so good) with this new Rails 3.1 option:

    config.action_dispatch.ignore_accept_header = true
    

    As mentioned in this Rails issue. That goes in config/application.rb.

    I tested it like so in a RSpec request test (using Capybara):

    it "should not break with HTTP_ACCEPT image/*;w=320;h=420 from iPhone" do
      page.driver.header "Accept", "image/*;w=320;h=420"
      visit "/some/path"
      page.should have_content("Some content")
    end
    
    0 讨论(0)
  • 2020-12-29 10:04

    I did this: In my controller, I put a before filter:

    def acceptable_mime_type
      unless request.accepts.detect{|a| a == :json || a == :html} || request.accepts.include?(nil)
        if request.accepts.detect{|a| a.to_s().include?("*/*")}
          ActionDispatch::Request.ignore_accept_header = true
        else
          render text: "Unacceptable", status: 406
          false
        end
      end
    end
    

    It checks for my supported types (e.g. json, html) and nil (nil renders default html), then if those mime types aren't supported, it checks for "/" in the header. If it finds it, I force rails to render the default mime type by ignoring the accept header.

    To test this in rspec, I had to do this:

    describe 'bad header' do
      describe 'accept' do
        let(:mimeTypes) { ["application/text, application/octet-stream"] }
    
        it 'should return a 406 status code' do
          request.accept = mimeTypes
          get 'index'
          expect(response.response_code).to eq 406
        end
    
        describe 'with */* included' do
          it 'should return a 200 status code' do
            request.accept = ["*/*"] + mimeTypes
            get 'index'
            expect(response.response_code).to eq 200
          end
        end
      end
    end
    

    For some reason, I was getting problems trying to send accept headers properly in rspec using the methods described here, but I found if I set request.accept to an Array, both my tests passed. Weird, I know, but I'm moving on to the next issue for now.

    0 讨论(0)
  • 2020-12-29 10:05

    I would be tempted to rescue the MissingTemplate in your application controller and log the Referrer header to see what's triggering this request. You never know, it might be some obscure part of your own app!

    If on the other hand you're confident this is being caused by a robot, have you considered adding the offending URL to your robots.txt file? For example:

    User-Agent: YandexImages
    Disallow: /your/failed/path
    

    Replacing 'your/failed/path' with the path that the robot is stumbling over. If the robot is struggling all over the place, you could just disallow access to the whole site for that particular robot:

    User-Agent: YandexImages
    Disallow: /
    

    I think this is a cleaner and lighter approach than implementing a handler specifically to suppress errors from a seemingly badly behaved bot.

    0 讨论(0)
  • 2020-12-29 10:09

    I agree about blocking the offending robot, but if you really want to force the response format add a before_filter and set request.format = :html, like this:

    before_filter :force_request_format_to_html
    
    private
    
    def force_request_format_to_html
      request.format = :html
    end
    
    0 讨论(0)
  • 2020-12-29 10:11

    Here is a stricter response; a suggestion by purp, from the discussion on issue 4127.

    class FooController
      rescue_from  ActionView::MissingTemplate, :with => :missing_template
    
      def missing_template
        render :nothing => true, :status => 406
      end
    end
    
    0 讨论(0)
提交回复
热议问题