1

少し助けが必要です。私は。。をしようとしています:

  1. リンク付きのメールを送信します。
  2. リンクに user.email_activation_token = true を設定してもらいます
  3. 次に、ホームページにリダイレクトします。

現在、メールへのリンクがありますが、このエラーが発生します。(私はHAMLを使用しています。)

編集:現在、ビューはありません。

不明なアクション

The action 'show' could not be found for UsersController

user_mailer/registration_confirmation

Confirm your email address please!

= accept_invitation_users_url({:token=>@user.email_activation_token})

users_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
        redirect_to root_url, :notice => "Signed up!"
    else
        render "new"
    end

    def accept_invitation
        @user = User.find_by_email_activation_token!(params[:token])
        @user.email_activation_token = true
        redirect_to root_url, :notice => "Email has been verified."
    end
  end
end

email_activations_controller.rb

class EmailActivationsController < ApplicationController
    def edit
        @user = User.find_by_email_activation_token!(params[:id])
        @user.email_activation_token = true
        save!
        redirect_to root_url, :notice => "Email has been verified."
    end
    def new
        @user = User.find_by_email_activation_token!(params[:id])
    end

    def edit
    end
end

user.rb (ユーザーモデル)

    class User < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation

      attr_accessor :password
      before_save :encrypt_password
      before_save { |user| user.email = email.downcase }
      before_create { generate_token(:auth_token) }
      # before_create { generate_token(:email_activation_token) }

      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
      VALID_PASSWORD_REGEX = /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,}$/
      validates_confirmation_of :password
      validates :password, :on => :create, presence: true, format: { with: VALID_PASSWORD_REGEX }
      validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

  def self.authenticate(email, password)
   user = find_by_email(email)
   if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
    user
   else  
    nil
  end
end

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

def generate_token(column)
  begin
    self[column] = SecureRandom.urlsafe_base64
  end while User.exists?(column => self[column])
end

end

ルート.rb

LootApp::Application.routes.draw do
  get "password_resets/new"

  get "sessions/new"

  resources :users
  resources :sessions
  resources :password_resets
  resources :email_activations

  resources :users do
    collection do 
      get :accept_invitation
    end 
  end

  # get "users/new"
  get "static_pages/home"
  get "static_pages/help"


  root to: 'static_pages#home'
  match "sign_up",  to: "users#new"
  match '/help',    to: 'static_pages#help'
  match '/log_in',  to: 'sessions#new'
  match '/log_out', to: 'sessions#destroy'
end
4

4 に答える 4

2

accept_invitation_users_url で ID の代わりにアクティベーション トークンを渡すのはなぜですか? このコードがある場合:

resources :users do
  member do 
    get :accept_invitation
  end 
end

ルートでは、accept_invitation は Restful ルートであり、その URL に id を与えると想定しています。http://guides.rubyonrails.org/routing.html#adding-more-restful-actionsを参照してください

だから、私はあなたがこのようなことをすべきだと思います:

ルート.rb

# resources :users ### Remove this as it is reduntant

resources :users do
  member do 
    get :accept_invitation
  end 
end

user_mailer/registration_confirmation

Confirm your email address please!

= accept_invitation_users_url(@user)

users_controller.rb

def accept_invitation
  @user = User.find(params[:id])
  @user.email_activation_token = true
  redirect_to root_url, :notice => "Email has been verified."
end

コメント読んだら

はい、@Frederick Cheung がコメントで述べたように、トークンの代わりに id を渡すと、電子メールの確認を送信するポイントが無効になり、実際に電子メールを受信しなくても電子メール アドレスを簡単に確認できるようになります。

したがって、@ PriteshJの回答を参照してください。どうやら、余分な行resources :usersを追加しただけでroutes.rb、私は大騒ぎしました:)。行を削除するだけでOKです。

于 2013-04-29T07:39:35.233 に答える
2

このようにルートを変更してみてください..

    resources :users do
      collection do 
        get :accept_invitation
      end 
    end

    resources :users
    resources :sessions
    resources :password_resets
    resources :email_activations
于 2013-04-29T07:14:45.850 に答える
2

routes.rb にはユーザー用のルートが 2 つあります。

削除する resources :users

最初のものresources usersは次のユーザー リソースをオーバーライドします

そのため、アプリケーションはcollectionルートがあることを認識せず、アクション用に生成された URL が次のようになるため、show アクション用accept_invitationとして扱います。id

/users/accept_invitation

show の署名と一致する/users/:id

最初の削除resources :usersはうまくいくはずです

また、email_activations_controller.rbの使用は見られません

安全に削除できるかもしれません

于 2013-04-29T07:17:30.617 に答える
2

resources :users を 2 回呼び出しました。

最初の呼び出しを削除する

于 2013-04-29T07:16:07.013 に答える