简体   繁体   中英

How do I fix ActionController::MissingExactTemplate: EventsController#index is missing a template for request formats: text/html

I am fixing some tests and the first line of the following test gives me this error ActionController::MissingExactTemplate: EventsController#index is missing a template for request formats: text/html.

test "#index should respond to the correct format" do
    get :index, params: {id: @event.id, format: :html} #<-- this line causes problems
    assert_response :success 

    get :index, params: {id: @event.id, format: :json}
    assert_response :success 
end

I believe the error refers to the following method in the controllers.

    def index
       @events = Event.upcoming.recent
       respond_to do |format|
         format.html # index.html.erb
         format.json { render json: @events }
       end
    end

I am starting to consider that the line is useless but I am still unsure.

If you make a json request, then format.json { render json: @events } will render the response.

If you make a HTML request, then format.html will look for a html file in app/views/event/index.html.erb

So, you need to create a file in the above location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM