【问题标题】:How do I fix ActionController::MissingExactTemplate: EventsController#index is missing a template for request formats: text/html如何修复 ActionController::MissingExactTemplate: EventsController#index 缺少请求格式的模板:text/html
【发布时间】:2020-07-10 20:09:10
【问题描述】:

我正在修复一些测试,以下测试的第一行给了我这个错误 ActionController::MissingExactTemplate: EventsController#index 缺少请求格式的模板: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

我认为错误是指控制器中的以下方法。

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

我开始认为这条线没用,但我仍然不确定。

【问题讨论】:

  • 正如稍后的错误状态,您需要在您的 app/views/events 文件夹中创建一个名为 index.html.erb 的文件

标签: ruby-on-rails ruby testing


【解决方案1】:

如果您发出 json 请求,则 format.json { render json: @events } 将呈现响应。

如果您发出HTML 请求,那么format.html 将在app/views/event/index.html.erb 中查找html 文件

所以,你需要在上面的位置创建一个文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2022-01-01
    • 2020-04-09
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多