自定义设计注册路由以接受 :invite_token 作为 rails 中 URL 的一部分

customize devise registration route to accept :invite_token as a part of URL in rails

我想自定义设计注册路由路径以在其末尾接受和可选:invitation_token。

例如

传入的 url 将是

http://mywebsite.com/tenants/sign_up/kEjte

这将使用邀请码来设置一些隐藏属性字段。

如果没有邀请码,则默认返回原始行为

我已经用自己的 RegistrationsController.rb

覆盖了注册控制器

这是我的路线

  devise_for :tenants  ,:controllers => { :registrations => "registrations" }

这是我搜索过的路线

 new_tenant_session GET    /tenants/sign_in(.:format)                           devise/sessions#new
          tenant_session POST   /tenants/sign_in(.:format)                           devise/sessions#create
  destroy_tenant_session DELETE /tenants/sign_out(.:format)                          devise/sessions#destroy
         tenant_password POST   /tenants/password(.:format)                          devise/passwords#create
     new_tenant_password GET    /tenants/password/new(.:format)                      devise/passwords#new
    edit_tenant_password GET    /tenants/password/edit(.:format)                     devise/passwords#edit
                         PATCH  /tenants/password(.:format)                          devise/passwords#update
                         PUT    /tenants/password(.:format)                          devise/passwords#update

cancel_tenant_registration GET /tenants/cancel(.:format) 注册#cancel tenant_registration POST /tenants(.:format) registrations#create new_tenant_registration GET /tenants/sign_up(.:format) 注册#new edit_tenant_registration GET /tenants/edit(.:format) 注册#edit 补丁/tenants(.:format) registrations#update PUT /tenants(.:format) 注册#update 删除/tenants(.:format) registrations#destroy

我已经通读了有关 Devise 的维基文章,但我无法理解。 谁能帮我一把?

编辑*

好的,所以我对路线做了一些修改

这里是修改后的路线

  devise_for :tenants  ,:controllers => { :registrations => "registrations" }


  devise_scope :tenants do
    get "/tenants/sign_up/:invitation_token" => "registrations#new"
  end

这里是推荐的路线

tenant_registration POST   /tenants(.:format)                                   registrations#create
 new_tenant_registration GET    /tenants/sign_up(.:format)                           registrations#new
edit_tenant_registration GET    /tenants/edit(.:format)                              registrations#edit
                         PATCH  /tenants(.:format)                                   registrations#update
                         PUT    /tenants(.:format)                                   registrations#update
                         DELETE /tenants(.:format)                                   registrations#destroy
                         GET    /tenants/sign_up/:invitation_token(.:format)         registrations#new

看起来路由现在接受邀请令牌,但是当我输入类似

的内容时
http://localhost:3000/tenants/sign_up/KjEid

Devises 给我以下错误

AbstractController::ActionNotFound at /tenants/sign_up/KjEid 
Could not find devise mapping for path "/tenants/sign_up/KjEid".This may      happen for two reasons:

什么给了?为什么它不接受令牌参数?

试试下面的代码片段。我假设您在注册控制器新操作中有代码来控制新参数。

devise_for :tenants  ,:controllers => { :registrations => "registrations" }  as :tenants do
    get 'sign_up/:invite_key' => 'registrations#new'
    get 'sign_up' => 'registrations#new'
end

我想出来了,应该是

  devise_for :tenants  ,:controllers => { :registrations => "registrations" }


  devise_scope :tenant do
    get "/tenants/sign_up/:invitation_token" => "registrations#new"
  end

我有devise_scope :tenantS do

之前,这就是设计抱怨的原因