【问题标题】:Ruby on Rails - Email confirmation link gives nomethoderrorRuby on Rails - 电子邮件确认链接给出 nomethoderror
【发布时间】:2013-04-25 01:19:12
【问题描述】:

我希望在用户注册时发送一封电子邮件。此电子邮件应包含一个链接将帐户更改为完整用户。我希望此电子邮件链接成为安全令牌。

  • email_token 是每个用户随机生成的令牌
  • email_activation_token 是一个布尔值,表示用户是否完成注册

目前:我收到了要发送的电子邮件,但是当我点击链接时出现此错误。

用户控制器中的 NoMethodError#accept_invitation

undefined method `email_activation_token=' for nil:NilClass

链接已发送 http://localhost:3000/users/accept_invitation.tLPOjM3hdA13rEv5FNhsOQ

user_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_token(params[:email_token])
      @user.email_activation_token = true
      @user.save
      redirect_to root_url, :notice => "Email has been verified."
  end
  end
end

registration_confirmation.html.haml

Confirm your email address please!

= accept_invitation_users_url(@user.email_token)

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_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 generate_token(column)
  begin
    self[column] = SecureRandom.urlsafe_base64
  end while User.exists?(column => self[column])
end

end

email_activations_controller.rb

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

    def edit
    end
end

【问题讨论】:

  • 对于 email_activation 资源,您的 routes.rb 文件是什么样的?我正在尝试做同样的事情,并且我真的很接近您的方法,但是我无法正确解决这个问题。你能帮我吗?谢谢!

标签: ruby-on-rails ruby ruby-on-rails-3 authentication email-validation


【解决方案1】:

这是一个快速的尝试:

= accept_invitation_users_url(email_token: @user.email_token)

链接http://localhost:3000/users/accept_invitation.tLPOjM3hdA13rEv5FNhsOQ 错误。

它应该类似于http://localhost:3000/users/accept_invitation?email_token=tLPOjM3hdA13rEv5FNhsOQ,以便成为params 哈希的一部分。

【讨论】:

  • 您如何生成该网址?我试了一下,我有resources :users { resources :verify_emails },它给了我一个名为edit_user_verify_email_url 的助手,但我必须将整个用户对象作为参数传递,而不仅仅是电子邮件令牌。我收到一条错误消息,提示“没有路由匹配 {:action=>"edit", :controller=>"verify_emails", :user_id=>#
猜你喜欢
  • 1970-01-01
  • 2013-04-23
  • 2013-04-19
  • 2013-04-22
  • 2015-06-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多