【问题标题】:RoR : Are devise controllers generated automatically or should we write on our own?RoR:设计控制器是自动生成的还是我们应该自己编写?
【发布时间】:2013-06-06 12:58:23
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    Devise 已经在幕后为您创建了所需的控制器。这些控制器中很少有:RegistrationControllerSessionController

    要自定义或覆盖任何控制器,请说RegistrationController;您可以执行以下操作(我的一个应用程序中的 sn-p):

    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

    【讨论】:

    • 我在这里找不到任何控制器。这些控制器是什么时候生成的?
    • 它使用 Devise gem 的控制器,这些控制器存在于您的 gem 配置中(并安装在您的应用程序中)。您不会直接在应用程序的 app/controllers/ 中看到它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多