【问题标题】:Override devise create controller action覆盖设计创建控制器操作
【发布时间】:2012-12-11 09:29:35
【问题描述】:

我正在创建一个应用程序(用户有很多组),现有用户可以在其中发送邀请以邀请人们加入组。如果 sign_up url 有一个邀请令牌,我需要覆盖默认的设计控制器操作以进行创建。

routes.rb

devise_for :users do 
  match '/users/sign_out', :to => 'devise/sessions#destroy'
  match '/users/sign_up/:invitation_token', :to => 'registrations#create'

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
#after_filter :add_user_to_group

def new
    super
end

def create
  if !params[:invitation_token].nil?
    token = params[:invitation_token]
    @invitation = Invitation.where "token" => token 
      if !@invitation.nil?
            build_resource
            if resource.save
              if resource.active_for_authentication?
                set_flash_message :notice, :signed_up if is_navigational_format?
                sign_up(resource_name, resource)
                respond_with resource, :location => after_sign_up_path_for(resource)
              else
                set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" 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 resource
             end
    else
      flash[:notice] = "Invalid Token. Cannot signup with this Link." 
    end
  else
    super
  end
end

def after_sign_up_path_for(resource_or_scope)
  @user = current_user
  raise @user.inspect
  @invitation = Invitation.where "recipient_email" => @user.email
  if !@invitation.nil?
    @user.update_attribute(:invitation_id, @invitation.id)
    @user_group = UserGroup.new
    @user_group.user_id = @user.id
    @userGroup.group_id = @invitation.group_id
    @userGroup.save!  
  end
  after_sign_in_path_for(resource)
end

结束

日志

Started GET "/users/sign_up/83003a4fab004ef4c1934c15f7215e8dc6a57718" for 127.0.0.1 at 2012-12-11 14:53:18
Processing by RegistrationsController#create as HTML
  Parameters: {"invitation_token"=>"83003a4fab004ef4c1934c15f7215e8dc6a57718"}
  SQL (0.1ms)  BEGIN
  User Exists (1.0ms)  SELECT 1 AS one FROM `users` WHERE `users`.`name` IS NULL LIMIT 1
  User Exists (0.1ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = '' LIMIT 1
   (0.1ms)  ROLLBACK

这会将我重定向到 sign_up 路径,但会在页面加载时执行验证。

Email can't be blank
Password can't be blank
Name can't be blank

sign_up 之后的日志与之前呈现的页面

Started POST "/users" for 127.0.0.1 at 2012-12-11 14:56:37 +0530
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"gUmNDb1Sj/ZVnXMG8t/Av0QlAYg7nAMTsSU5ZBW+s60=", "user"=>{"name"=>"Prashanth2", "email"=>"sample@sampleee.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  SQL (0.1ms)  BEGIN
  User Exists (0.4ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'sample@sampleee.com' LIMIT 1
  User Exists (0.3ms)  SELECT 1 AS one FROM `users` WHERE `users`.`name` = 'Prashanth2' LIMIT 1
  User Exists (0.2ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'sample@sampleee.com' LIMIT 1
  SQL (1.1ms)  INSERT INTO `users` (`created_at`, `current_sign_in_at`, `current_sign_in_ip`, `email`, `encrypted_password`, `invitation_id`, `invitation_limit`, `last_sign_in_at`, `last_sign_in_ip`, `name`, `remember_created_at`, `reset_password_sent_at`, `reset_password_token`, `sign_in_count`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Tue, 11 Dec 2012 09:26:37 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "sample@sampleee.com"], ["encrypted_password", "$2a$10$jcaLbYWNqp2sbUlAZWb38.Ls7Ku90ImOQ11k0fxJSq/mD4Zm2U1JO"], ["invitation_id", nil], ["invitation_limit", 5], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["name", "Prashanth2"], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["updated_at", Tue, 11 Dec 2012 09:26:37 UTC +00:00]]
   (108.5ms)  COMMIT
  SQL (0.1ms)  BEGIN
   (0.3ms)  UPDATE `users` SET `last_sign_in_at` = '2012-12-11 09:26:38', `current_sign_in_at` = '2012-12-11 09:26:38', `last_sign_in_ip` = '127.0.0.1', `current_sign_in_ip` = '127.0.0.1', `sign_in_count` = 1, `updated_at` = '2012-12-11 09:26:38' WHERE `users`.`id` = 12
   (127.8ms)  COMMIT

【问题讨论】:

    标签: ruby-on-rails authentication devise ruby-on-rails-3.2


    【解决方案1】:

    我不得不做类似的事情。我最终添加了用于将令牌添加到用户模型并具有单独的控制器操作来接受邀请的代码:

    用户.rb

    after_create :accept_invitation, if: :token
    
    private
      def accept_invitation
        # Or whatever you need to add the invitation token.
        permission = Permission.find_by_token(token)
        if permission
          permission.update_attributes!(user_id: id, accepted: true) 
          UserMailer.admin_accepted_invitation_email(permission).deliver
        end
      end
    

    registrations_controller.rb

    class Users::RegistrationsController < Devise::RegistrationsController
      layout "home"
    
      def accept_invitation
      @invitation = Permission.find_by_token!(params[:token])
      @user = User.new(token: @invitation.token)
    end
    end
    

    【讨论】:

      【解决方案2】:

      请点击链接无密码注册

      Registration without password :: device

      【讨论】:

      • 很抱歉,但我不认为这正是我想要做的。我想创建一个新用户。创建用户后我需要做一个@user.update_attribute(:invitation_id, @invitation.id)
      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 2012-07-18
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 2011-04-02
      相关资源
      最近更新 更多