【问题标题】:ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"api/v1/topics", :format=>:json}ActionController::UrlGenerationError: 没有路由匹配 {:action=>"index", :controller=>"api/v1/topics", :format=>:json}
【发布时间】:2018-09-03 11:42:07
【问题描述】:

我在文件夹 (api/v1/) 中有我的主题控制器

class Api::V1::TopicsController < ApplicationController
  def index
    @topics = Topic.all
    render json: @topics
  end
end

当我尝试为上述代码编写 rspec 时:

require 'rails_helper'
require 'spec_helper'

RSpec.describe Api::V1::TopicsController do
  describe "GET #index" do
    it "should return a successful response" do
      get :index, format: :json
      expect(response).to be_success
    end
  end
end

我收到错误:

ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"api/v1/topics", :format=>:json}.

但是我有正确的路线我不知道为什么它会这样显示。任何解决方案都是最受欢迎的。

我的路线如下:

Rails.application.routes.draw do
  namespace :api, defaluts: {format: :json} do
    namespace :v1 do
      resources :topics
    end
  end
end

【问题讨论】:

  • 也显示路线
  • 我已经设置了我的路线

标签: ruby-on-rails rspec


【解决方案1】:

路线中的错字: namespace :api, defaluts: {format: :json} => defaults

【讨论】:

    【解决方案2】:

    你有一个错字。 defaluts: {format: :json} 应该是 defaults: {format: :json}

    【讨论】:

      【解决方案3】:

      好吧,伙计们,我在“默认值”中有错字。每个人都会犯错字,所以你不必对我的问题投反对票。

      【讨论】:

      • 这里已经提到的不是一个,而是两个答案。您可以接受其中之一,而不是重复他们的陈述。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      相关资源
      最近更新 更多