2015-01-13 5 views
0

사이트/레일즈 앱에 로그인하기 전에 사용자가 자신의 사용자 계정을 확인해야하는 레일스 4.2.0 앱에서 로직을 설정하려고합니다. 기본적으로, 나는 사람이 이메일/패스워드를 입력하고 그들의 서명을 할 수있는 sign up 양식을 가지고있다. 이 과정에서 확인 토큰을 사용하여 이메일 주소로 이메일을 보내면 확인을위한 링크가 제공됩니다. 확실하게 확인 토큰을 사용하여 DB의 부울 값을 false에서 true로 변경합니다. 나는 지금까지 구현 한 것을 게시 할 것이다.레일을 사용하여 확인 이메일을 보내는 방법은 무엇입니까?

users_controller.rb

def create 
      @user = User.new(user_params) 
      if @user.save 
       # send confirmation email after user has been created. 
       @user.send_confirmation 

       session[:user_id] = @user.id 
       redirect_to root_url, notice: "Thank you for signing up!" 
      else 
       render "new" 
      end 
     end 


def confirm 
     @user = User.find_by_confirmation_token!(params[:id]) 
     if @user.update_attributes(confirmed: true) 
      redirect_to login_path 
     end 
    end 

confirmation.text.erb

To confirm your account, click the URL below. 

<%= user_url(@user.confirmation_token) %> 

<%= url_for(controller: 'users', action: 'confirm') %> 

If you did not request your account creation, just ignore this email and your account will not be created. 

routes.rb

Rails.application.routes.draw do 

    resources :articles do 
    resources :comments 
    end 

    get 'resume' => 'resume#index' 

    get 'signup' => 'users#new' 
    get 'login' =>'sessions#new' 
    get 'logout' => 'sessions#destroy' 

    # the below route led to a rails routing error 
    # get 'confirm' => 'users/:confirmation_token#confirm' 

    resources :users 
    resources :sessions 
    resources :password_resets 

    # route to hopefully get confirmation link working :-/ 
    match '/users/:confirmation_token', :to => 'users#confirm', via: [:post, :get] 

    # test route 
    match 'users/foo', :to => 'users#foo', via: [:post, :get] 

    root "articles#index" 

    # Added below route for correct "resumé" spelling 
    get 'resumé', :to =>"resume#index" 

    # get 'about#index' 
    get 'about' => 'about#index' 
    get 'contact' => 'contact#contact' 

    resources :about 
    resources :contact 

    match ':controller(/:action(/:id))(.:format)', via: [:post, :get] 
+0

다음과 같습니다/plataformatec/devise/master/Devise/Models/Confirmable – kasperite

답변

0

내가 엔데 D까지, 즉 멀리이 날 나를 확인을 편집 할 수 내 routes.rb

resources :confirmations 

에 다음 줄을 추가 허용 users_controller.rb에서, 그 자체가 컨트롤러에 확인 로직을 분리 .text.erb 및 이메일 메시지에 다음 링크를 넣어,

<%= edit_confirmation_url(@user.confirmation_token) %> 

사람이, 확인 컨트롤러의 편집 액션, t에 그 경로를 자신의 계정을 확인하는 이메일을 수신하여

그는 액션을 호출하여 업데이트 액션을 호출하고 계정을 확인합니다. ? http://www.rubydoc.info/github : 컨트롤러가 고안 사용하지 않는 이유는 그들이위한 확 모듈을 구현하는 방법을 살펴 수

confirmations_controller.rb

class ConfirmationsController < ApplicationController 

    def new 
    end 

    def edit 
     @user = User.find_by_confirmation_token!(params[:id]) 
     update 
    end 

    def update 
     # @user = User.find_by_confirmation_token!(params[:id]) 
     if @user.confirmation_sent_at < 2.hours.ago 
      redirect_to new_confirmation_path, :alert => "Confirmation has expired." 
     # elseif @user.update_attributes(params[:user]) 
     elsif @user.update_attributes(confirmed: true) 
      redirect_to root_url, :notice => "Your account has been confirmed." 
     else 
      render :new 
     end 
    end 
end 
관련 문제