没有路由匹配...... Rails 引擎 [英] No Route Matches ... Rails Engine

查看:32
本文介绍了没有路由匹配...... Rails 引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不断收到错误:

没有路由匹配 {:action=>"create", :controller=>"xaaron/api_keys"}

在测试中抛出了什么:

it "should not create an api key for those not logged in" do
  post :create
  expect(response).to redirect_to xaaron.login_path
end

当我转到 spec/dummy 并运行 rake routes 命令时,我看到:

when I go to spec/dummy and run the rake routes command I see:

       api_keys GET    /api_keys(.:format)                 xaaron/api_keys#index
                POST   /api_keys(.:format)                 xaaron/api_keys#create
    new_api_key GET    /api_keys/new(.:format)             xaaron/api_keys#new
   edit_api_key GET    /api_keys/:id/edit(.:format)        xaaron/api_keys#edit
        api_key GET    /api_keys/:id(.:format)             xaaron/api_keys#show
                PATCH  /api_keys/:id(.:format)             xaaron/api_keys#update
                PUT    /api_keys/:id(.:format)             xaaron/api_keys#update
                DELETE /api_keys/:id(.:format)             xaaron/api_keys#destroy

这表明是的,这条路线确实存在.我的这个引擎的路由文件看起来像:

Which shows that yes this route does exist. My routes file for this engine looks like:

Xaaron::Engine.routes.draw do
      get 'login' => 'sessions#new', :as => 'login'
      get 'logout' => 'sessions#destroy', :as => 'logout'
      get 'signup' => 'users#new', :as => 'signup'
      get 'permission_denied' => 'error#denied', :as => 'permission_denied'
      get 'record_not_found' => 'error#error', :as => 'record_not_found'
      get 'password_reset' => 'password_resets#edit', :as => 'rest_user_password'

      resource :error, controller: 'error'

      resources :users
      resources :api_keys
      resources :sessions
      resources :roles
      resources :password_resets
end

我错过了什么?

对于那些好奇我如何获得这些路由的人,这是因为虚拟应用程序的路由文件是这样设置的(默认情况下):

For those of you curious how I am getting these routes, its because the dummy app's routes file is set up (by default) as such:

Rails.application.routes.draw do

  mount Xaaron::Engine => "/xaaron"
end

更新二

我一直在阅读这个关于如何在引擎中完成路由的api文档,我相信这种方式我这样做是正确的,控制器是如何定义的:

Update II

I have been reading this api docs on how routing is done in engines and I believe the way I have done this is correct, how ever the controller is defined as such:

module Xaaron
  class ApiKeysController < ActionController::Base
    before_action :authenticate_user!

    def index
      @api_key = Xaaron::ApiKey.where(:user_id => current_user.id)
    end

    def create
      @api_key = Xaaron::ApiKey.new(:user_id => current_user.id, :api_key => SecureRandom.hex(16))
      create_api_key(@api_key)
    end

    def destroy
      Xaaron::ApiKey.find(params[:id]).destroy
      flash[:notice] = 'Api Key has been deleted.'
      redirect_to xarron.api_keys_path
    end
  end
end

推荐答案

你需要告诉你的规范你正在使用引擎路由:

You need to tell your spec you are using the engine routes:

describe ApiKeysController do
  routes { Xaaron::Engine.routes }

  it "should not create an api key for those not logged in" do
    post :create
    expect(response).to redirect_to xaaron.login_path
  end
end

这篇关于没有路由匹配...... Rails 引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆