【问题标题】:ActionController::UrlGenerationError - with route defined and action in controller, still getting error for no routeActionController::UrlGenerationError - 定义了路由并在控制器中执行操作,仍然因没有路由而出错
【发布时间】:2015-07-22 16:03:37
【问题描述】:

运行我的schools_controller_spec.rb 测试时,我在RSpec 中遇到以下错误:

ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"schools"}

让我感到困惑的是,我已经配置了路由,并且在适当的控制器中定义了操作。对于规范中的其他测试,我没有收到此错误,例如'GET #index' 等。使用 RSpec/Capybara 运行 Rails 4.2。

这是 routes.rb:

Rails.application.routes.draw do
  root to: 'pages#home', id: 'home'
  resources :users
  resources :schools
  resource :session, only: [:new, :create, :destroy]
  match '/home',      to: 'pages#home', via: 'get', as: 'home_page'
end

rake 路由返回:

    schools GET    /schools(.:format)          schools#index
            POST   /schools(.:format)          schools#create
 new_school GET    /schools/new(.:format)      schools#new
edit_school GET    /schools/:id/edit(.:format) schools#edit
     school GET    /schools/:id(.:format)      schools#show
            PATCH  /schools/:id(.:format)      schools#update
            PUT    /schools/:id(.:format)      schools#update
            DELETE /schools/:id(.:format)      schools#destroy

在第五行定义了路线,如学校#show。

schools_controller.rb:

class SchoolsController < ApplicationController
  before_action :require_signin
  before_filter :admin_only, except: :index, :show

  def index
    @schools = School.all
  end

  def show
   # code pending
  end

  private

    def admin_only
      unless current_user.admin?
        redirect_to :back, alert: "Access denied."
      end
    end
end

各个学校的链接似乎在视图助手 (_school.html.haml) 中正确定义:

%li#schools
  = link_to school.name, school
  = school.short_name
  = school.city
  = school.state

查看前端 HTML 确认它工作正常。我可以看到,例如:&lt;a href="/schools/1"&gt;Community College of the Air Force&lt;/a&gt;。当我单击该链接时,页面在调试转储中显示以下内容:

--- !ruby/hash:ActionController::Parameters
controller: schools
action: show
id: '1'

最后,为了更好的衡量,这里是规范文件 (schools_controller_spec.rb):

require 'rails_helper'

describe SchoolsController, type: :controller do
  # specs omitted for other actions

  describe 'GET #show' do
    context "when not signed in" do
      it "returns a 302 redirect code" do
        get :show
        expect(response.status).to eq 302
      end

      it "redirects to the signin page" do
        get :show
        expect(response).to redirect_to new_session_path
      end
    end

    context "when signed in as user" do
      before :each do
        @user = double(:user)
        allow(controller).to receive(:current_user).and_return @user
        @school = create(:school)
      end

      it "assigns the school to the @school variable" do
        get :show
        expect(assigns(:school)).to eq @school
      end
    end
  end
end

路由出现在 rake 路由中。该方法在适当的控制器中定义。似乎没有任何愚蠢的命名错误(例如复数/单数)。例如,该规范似乎没有任何问题路由 GET #index 或其他路由。一切都完全在浏览器中按预期工作。

那么为什么我在运行控制器规范时总是收到“无路由匹配”错误?

【问题讨论】:

  • 这是因为 show 操作需要一个 id,而您没有在测试中提供。
  • 该死!感觉很愚蠢,我没有看到……看到错误消息,它没有提到“id”,并且没有考虑清楚。您想将其作为回复而不是评论发布,以便我接受它作为答案吗?
  • 经常这样!很高兴就是这样,现在我不在工作我写了一个答案。

标签: ruby-on-rails rspec


【解决方案1】:

这是因为 show 操作需要一个您当前没有传递的 id。替换:

get :show

有了这个:

get :show, id: school.id

以上假设您有一个学校变量,也许是一个前块中的让?

let(:school) { create(:school) }

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多