ruby-on-rails - 错误率: Are devise controllers generated automatically or should we write on our own?

标签 ruby-on-rails devise

我是 ruby​​ on Rails 的新手。我正在尝试使用 devise gem 进行身份验证。我正在浏览 github 中的教程。我使用rails generated devise:views 创建了设计 View 。但我没有找到任何 Controller 。我需要自己创建还是有任何命令可以为其生成 Controller ? 请帮忙

最佳答案

Devise 已在幕后为您创建所需的 Controller 。这些 Controller 中很少有:RegistrationControllerSessionController

要自定义或覆盖任何 Controller ,请说RegistrationController;您可以执行以下操作(我的一个应用程序的片段):

class RegistrationsController < Devise::RegistrationsController
  before_filter :admin_user, :only => [:destroy]

  def new
    super
  end

  def create
    if simple_captcha_valid? #verifying user registration by captcha
      super
    else
      build_resource
      clean_up_passwords(resource)
      flash.now[:alert] = "There was an error with the captcha code below. Please re-enter the code."      
      render :new
    end
  end

  def update
    # required for settings form to submit when password is left blank
    if params[:user][:password].blank?
      params[:user].delete("password")
      params[:user].delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(params[:user])
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to rooth_path
  end
end

更多内容可以关注:https://github.com/plataformatec/devise#configuring-controllers

关于ruby-on-rails - 错误率: Are devise controllers generated automatically or should we write on our own?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962746/

相关文章:

ruby-on-rails - Rails 路由自定义用户 Controller 和设计 gem 的正确方法

ruby-on-rails - Rails/Devise - 我应该用 devise 和 rspec 测试什么?

ruby-on-rails - 带条件的 Rails as_json

ruby-on-rails - Rails:记录的自定义排序

mysql - 哪些工具通常使用 Ruby on Rails 开发人员来处理 MySQL 的管理

ruby-on-rails - 设计:用户模型中的 authentication_token 列为空

ruby-on-rails - 设计和认证

ruby-on-rails - link_to href 中的不安全参数值

ruby-on-rails - 重定向 ruby​​ 脚本的 shell 输出

devise - 带有设计的omniauth google-oauth2-invalid_credentials和 "Csrf detected"