继承自 Devise::SessionsController 的控制器中自定义操作的路由

发布于 2024-12-09 09:16:46 字数 941 浏览 0 评论 0原文

我有一个继承自 Devise::SessionsController 的会话控制器:

class SessionsController < Devise::SessionsController

  skip_before_filter :authenticate_user!, :only => [:get_token]

  def create
   ....
  end

 def destroy
  ...
 end

 def get_token
   response.headers["app-key"] = form_authenticity_token()
   render :text=>'Token Set'
 end

end

正如您在上面看到的,我正在覆盖创建和销毁操作,并且我添加了另一个名为 get_token 的操作。我为其添加了路线,如下所示:

Routes.rb

Application.routes.draw do

  devise_for :users, :controllers => { :sessions => "sessions" }, :path => "users",      :path_names => { :sign_in => 'login', :sign_out => 'logout',:confirmation => 'verification'}

  match 'get_token', :to => 'sessions#get_token'

但是当我尝试访问 get_token 方法时出现以下错误;

[Devise] Could not find devise mapping for path "/get_token". 

如何为 get_token 操作添加路由。

提前致谢

I have a session controller which inherits from Devise::SessionsController:

class SessionsController < Devise::SessionsController

  skip_before_filter :authenticate_user!, :only => [:get_token]

  def create
   ....
  end

 def destroy
  ...
 end

 def get_token
   response.headers["app-key"] = form_authenticity_token()
   render :text=>'Token Set'
 end

end

As you can see above i am overwriting create and destroy action and i have added another action named get_token. I added routes for it as shown below:

Routes.rb

Application.routes.draw do

  devise_for :users, :controllers => { :sessions => "sessions" }, :path => "users",      :path_names => { :sign_in => 'login', :sign_out => 'logout',:confirmation => 'verification'}

  match 'get_token', :to => 'sessions#get_token'

But I am getting the following errror when i am trying to access get_token method;

[Devise] Could not find devise mapping for path "/get_token". 

How to add route for the get_token action.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2024-12-16 09:16:46

您需要像这样在 Devise 中确定路由范围:

devise_scope :user do
  get 'get_token' => 'sessions#get_token'
end

这应该允许您调用 http://your-url/get_token访问该操作。

You need to scope the route in Devise like so:

devise_scope :user do
  get 'get_token' => 'sessions#get_token'
end

That should allow you call http://your-url/get_token to access that action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文