Missing template blogs/index on Ruby on Rails project

前端 未结 6 1366
野的像风
野的像风 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 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.

提交回复
热议问题