代码之家  ›  专栏  ›  技术社区  ›  Amin Shah Gilani

actionController::URLGenergonerror for device注册控制器

  •  1
  • Amin Shah Gilani  · 技术社区  · 7 年前

    我遇到了一个有趣的 ActionController::UrlGenerationError 在测试我的定制设计时 User::RegistrationsController

    错误如下:

     ActionController::UrlGenerationError:
       No route matches {:action=>"create", :controller=>"user/registrations", :user=>{:name=>"Adriane Koepp", :city=>"Nidiafurt", :address=>"14955 Cormier Viaduct", :country=>"Mozambique", :email=>"moshe@kozey.com", :phone_primary=>"(295) 491-0447 x9108", :phone_secondary=>"536.985.9499 x7264", :postal_code=>"93438-7448", :province=>"South Carolina", :password=>"MaH9R5G8XqB", :pets_attributes=>[{:name=>"Patches", :chip_number=>"149793073311890", :species=>"iusto"}]}}
    

    这个错误出现在我对这个控制器的所有测试中,但是为了简单起见,我只列出一个测试 create 行动:

    it 'creates a new User' do
      expect do
        post :create, params: { user: valid_attributes }
      end.to change(User, :count).by(1)
    end
    

    这个 routes.rb 包含:

    devise_for :users, controllers: {
      registration: 'user/registration'
    }
    

    我可以导航到我的注册页面 http://localhost:3000/users/sign_up ,但出于某种原因,我的测试似乎不认为此控制器具有 创造 行动。为什么呢?

    其他步骤

    根据d1ceward的建议,我在路由中使用了复数形式的“注册”,现在错误消息变为 AbstractController::ActionNotFound 错误。我通过跟踪 documentation 并将以下块添加到我的测试的顶部:

    before(:each) do
      @request.env['devise.mapping'] = Devise.mappings[:user]
    end
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   D1ceWard    7 年前

    你的错误是由于缺少复数形式, devise 不知道什么 registration 是,但与 registrations .

    解决方案:

    # routes.rb
    devise_for :users, controllers: {
      registrations: 'user/registrations'
    }
    

    你可以使用 rails routes 检查所有现有路线。

    Devise doc about custom controllers