سؤال

Currently I have ActionMailer send an email when a user registers, and I generate a random :sign_in_token with the user.

How can a user then click on the link sent to his email and update the users :registration_complete boolean value to TRUE?

Currently, I am able to send the link and generates a random token, but I don't know how to update the boolean value through the email.

MODELS

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :sign_in_token, 
              :registration_complete

  ###This generates my sign_in_token
  def generate_sign_in_token
    self.sign_in_token = Digest::SHA1.hexdigest([Time.now, rand].join)
  end

end

CONTROLLER

def create
  @user = RegularUser.new(params[:regular_user])
  if @user.save      

    ###Sends the User an email with sign_in_token
    UserMailer.registration_confirmation(@user, login_url+"/#{@user.sign_in_token}").deliver

    flash[:success] = "Please Check Your Email to Verify your Registration!"
    redirect_to (verifyemail_path)
  else
    render 'new'
  end
end

USER_MAILER

def registration_confirmation(user, login_url)
  @login_url = login_url
  @user = user
  mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to APP")
end

VIEWS

###Redirects User to Login Page, But how do i connect it to my activate method?
<%= link_to "Complete Registration", @login_url %>

ROUTES

match '/login/:sign_in_token', :to => 'sessions#new'
هل كانت مفيدة؟

المحلول

When they click a link, it takes them to a controller with an action of set_complete using a GET request, which sets the boolean value.

Something like:

def set_complete
user = User.find(params[:user])
user.update_attribute(registration_complete => true)
redirect_to login_path # or whatever your login url is...

end

For the controller action and something like this for the link:

<a href="example.com/registrations/set_complete?user=1" />

Here is a sample of what might go in the routes file:

get "/users/set_complete", :to => "users#set_complete"

You'd probably need to set the user id to whatever you want using erb, andmake a few other app-specific customizations, but this is the general idea.

Hope this helps!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top