【问题标题】:How to add additional functions to devise controllers如何添加附加功能来设计控制器
【发布时间】:2019-05-11 11:32:31
【问题描述】:

我正在使用 Devise 进行学生身份验证,并且我还有其他名为 show_profileedit_profile 的操作,因此学生可以查看和编辑他的个人资料。

问题是我改写的控制器写入了 Devise 控制器,因此登录/注册停止工作。如何使我的控制器成为 Devise 控制器的扩展?

如果我把这两个:

class Students::RegistrationsController < Devise::RegistrationsController
class StudentsController < ApplicationController

并在登录class StudentsController &lt; ApplicationController 时评论此内容,登录后此class Students::RegistrationsController &lt; Devise::RegistrationsController 有效。

class Students::RegistrationsController < Devise::RegistrationsController
#class StudentsController < ApplicationController

     private
    def secure_params
      params.require(:student).permit(:name, :father_name, :grand_father_name)
    end
   public
     before_action :authenticate_student!
   def show_profile
        @student = current_student
     end

   def edit_profile
        @student = current_student
   end

   def update_profile
   @student = current_student

    if @student.update_attributes(secure_params)
      redirect_to(:action => 'show_profile',:id => @student.id )
      flash[:notice] = "student edited successfully"
    else
      render('edit_profile')
    end
   end
end

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    您必须告诉 Devise 您在 routes.rb 中使用自定义设置 像这样,根据您的需要进行调整。

    devise_for :users,
      controllers: {
        registrations: 'student/registrations'
      }
    

    如果你的模型是这样命名的,那么显然使用devise_for :students

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多