输入的电子邮件覆盖SecureRandom电子邮件?

时间:2016-03-16 22:22:25

标签: ruby-on-rails ruby facebook email

enter image description here

方法#1

Send email reminder to <%= email_field_tag :email,  nil, placeholder: 'Enter Email', class: "send-email" %> # :email belongs to the users table

方法#2

Send email reminder to <%= f.email_field :default_email, class: "send-email", placeholder: "Enter Email" %> # :default_email belongs to the challenges table. The point of this is to try something in the create process like current_user.email == @challenge.default_email

然后将用户重定向到signup url

enter image description here

如果用户通过Facebook注册,则会生成随机电子邮件,以便他可以完成验证过程:user.email = SecureRandom.hex + "@mailinator.com"

user.rb

  def self.from_omniauth(auth)
     # Sets 60 day auth token
     oauth = Koala::Facebook::OAuth.new("125402372971231236229929", "ee917abf2e8f1c98274cdafssadffddffaebb1346f4")
     new_access_info = oauth.exchange_access_token_info auth.credentials.token

     new_access_token = new_access_info["access_token"]
     new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds

    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.image = auth.info.image
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = new_access_token # auth.credentials.token <- your old token. Not needed anymore.
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.password = (0...8).map { (65 + rand(26)).chr }.join
      user.email = SecureRandom.hex + "@mailinator.com" unless user.email.present?
      user.activated = true
      user.save!
    end
  end

但是,如何才能使用户在上面显示的 challenge / create.html.erb 表单中输入的电子邮件覆盖SecureRandom电子邮件?

users_controller

  def create
    @user = User.new(user_params)
    if @user.save
      action = session.delete(:challenge_action)
      deadline = session.delete(:challenge_deadline)
      committed = session.delete(:challenge_committed)
      date_started = session.delete(:challenge_date_started)
      order = session.delete(:challenge_order)
      days_challenged = session.delete(:challenge_days_challenged)
      why = session.delete(:challenge_why)
      conceal = session.delete(:challenge_conceal)
      @user.challenges.create(action: action, deadline: deadline, why: why, conceal: conceal, date_started: date_started, committed: committed, days_challenged: days_challenged)
      end
      redirect_to root_url
    else
      render 'new'
    end
  end

challenges_controller

  before_action :update_user_email, if: proc {|c| c.current_user.present? && c.params[:email].present? }

  def create
    @challenge = Challenge.new(challenge_params)
    if params[:step] == '2'
      if current_user == nil
        # If there is no user, store values to the session.
        session[:challenge_action] = challenge_params[:action]
        session[:challenge_committed] = challenge_params[:committed]
        session[:challenge_deadline] = [params["challenge"]["deadline(3i)"], params["challenge"]["deadline(2i)"], params["challenge"]["deadline(1i)"]].join('/')
        session[:challenge_date_started] = [params["challenge"]["date_started(3i)"], params["challenge"]["date_started(2i)"], params["challenge"]["date_started(1i)"]].join('/')
        session[:challenge_order] = challenge_params[:order]
        session[:challenge_days_challenged] = challenge_params[:days_challenged]
        session[:challenge_why] = challenge_params[:why]
        session[:challenge_conceal] = challenge_params[:conceal]
        redirect_to signup_path
      else
        @challenge = current_user.challenges.build(challenge_params)
        @challenge.save
        redirect_to root_path
      end
    else
      respond_modal_with @challenge
    end
  end

private

  def update_user_email
    email = params[:email]
    current_user.update_attribute(:email, email)
  end

sessions_controller

class SessionsController < ApplicationController

  def new
  end

  def facebook
    user = User.from_omniauth(env["omniauth.auth"])
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
    redirect_to root_url
  end

  def create
    assign_email(cookies[:challenges_email])
    user = User.find_by(email: params[:session][:email].downcase)
    assign_email(cookies[:challenges_email])
    if user && user.authenticate(params[:session][:password])
      assign_email(cookies[:challenges_email])
      log_in user
      params[:session][:remember_me] == 'nil' ? forget(user) : remember(user)
      redirect_to root_url
    else
      flash.now[:info] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

2 个答案:

答案 0 :(得分:1)

@joseph已经回答了这个问题非常简单,但我认为你可以通过在控制器中设置电子邮件来获取电子邮件; User.new(:email => "email you want to show in the field")  无需传递nil用户对象传递新对象。

在视图文件中,它将显示在带有

的电子邮件字段中
Send email reminder to <%= f.email_field :email, class: "send-email", placeholder: "Enter Email" %>

答案 1 :(得分:1)

我们谈到在另一个问题中将其设置为cookie。如果你采用这种方法,你可以将cookie传递给方法。类似于这个问题中讨论的内容:

 before_create :set_user_email

 def assign_email(email)
   @email = email
 end

 def set_user_email
   self.email = @email
 end

然后在您可以访问cookie的控制器操作中:

 assign_email(cookies[:challenge_email])