zoukankan      html  css  js  c++  java
  • devise /users/edit controller中加入自定义变量

    自定义devise Registration
      #devise_for :users
      devise_for :users,  :controllers => { :registrations => "registrations" } do
      #devise_for :users do
        get "/register", :to => "devise/registrations#new"
        get "/login", :to => "devise/sessions#new"
        get "/logout", :to => "devise/sessions#destroy"
        get "/settings/account", :to=>"devise/registrations#edit"
      end

    # coding: utf-8
    class RegistrationsController < ApplicationController
      prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
      prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
      include Devise::Controllers::InternalHelpers

      # GET /resource/sign_up
      def new
        resource = build_resource({})
        respond_with_navigational(resource){ render_with_scope :new }
      end

      # POST /resource
      def create
        build_resource

        if resource.save
          if resource.active_for_authentication?
            set_flash_message :notice, :signed_up if is_navigational_format?
            sign_in(resource_name, resource)
            respond_with resource, :location => redirect_location(resource_name, resource)
          else
            set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
            expire_session_data_after_sign_in!
            respond_with resource, :location => after_inactive_sign_up_path_for(resource)
          end
        else
          clean_up_passwords(resource)
          respond_with_navigational(resource) { render_with_scope :new }
        end
      end

      # GET /resource/edit
      def edit
        @view_name="account"
        render_with_scope :edit
      end

      # PUT /resource
      # We need to use a copy of the resource because we don't want to change
      # the current user in place.
      def update
        self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)

        if resource.update_with_password(params[resource_name])
          set_flash_message :notice, :updated if is_navigational_format?
          sign_in resource_name, resource, :bypass => true
          respond_with resource, :location => after_update_path_for(resource)
        else
          clean_up_passwords(resource)
          respond_with_navigational(resource){ render_with_scope :edit }
        end
      end

      # DELETE /resource
      def destroy
        resource.destroy
        Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
        set_flash_message :notice, :destroyed if is_navigational_format?
        respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
      end

      # GET /resource/cancel
      # Forces the session data which is usually expired after sign
      # in to be expired now. This is useful if the user wants to
      # cancel oauth signing in/up in the middle of the process,
      # removing all OAuth session data.
      def cancel
        expire_session_data_after_sign_in!
        redirect_to new_registration_path(resource_name)
      end

      protected

        # Build a devise resource passing in the session. Useful to move
        # temporary session data to the newly created user.
        def build_resource(hash=nil)
          hash ||= params[resource_name] || {}
          self.resource = resource_class.new_with_session(hash, session)
        end

        # The path used after sign up. You need to overwrite this method
        # in your own RegistrationsController.
        def after_sign_up_path_for(resource)
          after_sign_in_path_for(resource)
        end

        # Overwrite redirect_for_sign_in so it takes uses after_sign_up_path_for.
        def redirect_location(scope, resource)
          stored_location_for(scope) || after_sign_up_path_for(resource)
        end

        # Returns the inactive reason translated.
        def inactive_reason(resource)
          reason = resource.inactive_message.to_s
          I18n.t("devise.registrations.reasons.#{reason}", :default => reason)
        end

        # The path used after sign up for inactive accounts. You need to overwrite
        # this method in your own RegistrationsController.
        def after_inactive_sign_up_path_for(resource)
          root_path
        end

        # The default url to be used after updating a resource. You need to overwrite
        # this method in your own RegistrationsController.
        def after_update_path_for(resource)
          if defined?(super)
            ActiveSupport::Deprecation.warn "Defining after_update_path_for in ApplicationController " <<
              "is deprecated. Please add a RegistrationsController to your application and define it there."
            super
          else
            after_sign_in_path_for(resource)
          end
        end

        # Authenticates the current scope and gets the current resource from the session.
        def authenticate_scope!
          send(:"authenticate_#{resource_name}!", true)
          self.resource = send(:"current_#{resource_name}")
        end
    end


    更改layout
    class ApplicationController < ActionController::Base
      protect_from_forgery
      layout :specify_layout

      protected

        def specify_layout
          controller_name == 'registrations' && action_name == 'edit' ? 'settings' : 'devise'
        end
    end


  • 相关阅读:
    luogu P1073 最优贸易 |分层图最短路
    luogu P1901 发射站 |单调队列
    luogu P1759 通天之潜水 |背包
    luogu P1801 【黑匣子_NOI导刊2010提高(06)】|堆+分块
    bzoj1642[Usaco2007 Nov]Milking Time 挤奶时间*
    bzoj1616[Usaco2008 Mar]Cow Travelling游荡的奶牛*
    bzoj1623[Usaco2008 Open]Cow Cars 奶牛飞车*
    bzoj1612[Usaco2008 Jan]Cow Contest奶牛的比赛*
    bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支*
    bzoj1601[Usaco2008 Oct]灌水*
  • 原文地址:https://www.cnblogs.com/lexus/p/2147815.html
Copyright © 2011-2022 走看看