成功登录后重定向到设备控制器

时间:2014-05-13 14:30:04

标签: ruby-on-rails ruby redirect devise

我是Ruby On Rails的新手。我正在使用devise gem制作应用程序。我的要求是在成功登录后我应该重定向再次设计控制器。

我已将设计控制器创建为“用户”

我已经创建了一个用于重定向的控制器home_controller.rbhome控制器下我编写了这个。

def index
  if user_signed_in?
    redirect_to :controller => 'users', :action =>add
  end
end

我在add

下编写了users_controller.rb方法

routes.rb下我编码了这个

devise_for :users, controllers:{sessions: "users/sessions"}
root :to => 'home#index'
match 'users/:action' => 'users#add', :as => :add

但它没有重定向。我该怎么办。任何帮助。感谢

2 个答案:

答案 0 :(得分:1)

试试这个: -

resources :users do
  member do
    get "add"
  end
end

答案 1 :(得分:0)

如果我理解正确,您会想要使用Devise redirect helpers

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def after_sign_in_path_for(resource)
      user_add_path
   end
end

我不明白这是:

  

我应该重定向再次设计控制器

Devise controllers分为SessionsControllerRegistrationsControllerPasswordsControllerConfirmationsController&amp; UnlocksController

您想将哪一个重定向到?

我的预感,在阅读您的评论后这会得到加强,您是否想要重定向到UsersController,就像这样:

#config/routes.rb
resources :users, only: :show do
   get :add
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def add
      //declarations here
   end

   def show
      //declarations here
   end
end

这应该可以帮到你

相关问题