"xaaron/api_keys"} 测试中抛出的是: it "should not create-6ren">
gpt4 book ai didi

ruby-on-rails - 没有路由匹配... Rails 引擎

转载 作者:数据小太阳 更新时间:2023-10-29 06:39:03 26 4
gpt4 key购买 nike

所以我不断收到错误:

没有路由匹配 {: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 命令时,我看到:

       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

这表明是的,这条路线确实存在。该引擎的路由文件如下所示:

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

我错过了什么?

更新

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

Rails.application.routes.draw do

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

更新二

我一直在读this api docs on how routing is done in engines我相信我这样做的方式是正确的,无论 Controller 是如何定义的:

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

最佳答案

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

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

关于ruby-on-rails - 没有路由匹配... Rails 引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com