lundi 27 avril 2020

confirmation token Automatically generated NIL

Hello guys, I have an issue with automatically generated token. In a model, i generate token automatically like this:

class User < ApplicationRecord
  before_create :generate_confirm_token

  def generate_confirm_token
    self.confirm_token = generate_token
  end

  def generate_token
    loop do
      token = SecureRandom.hex(10)
      break token unless User.where(confirm_token: token).exists?
    end
  end

That works fine for me, after creating of user, the token is generated. But the issue is in a Controller:

class Companies::StudentsController < ApplicationController


  def create
    @company = Company.find(params[:company_id])
    @student = @company.students.create(student_params)
     raise @student.inspect
    if @student.save
      StudentMailer.with(student: @student).welcome_email.deliver_now
      redirect_to company_students_path
    else
      render :new
    end
  end

Object student, contains confirm_token BUT in a params, the confirm token is empty!

enter image description here I need the token in params because in next step (mailer) i use Find_by(params[:confirm_token]) Here is how use a confirm_token in my view (i assume i need the confirm_token in params so i have to have it in a view also):

  <%= f.hidden_field :confirm_token %>

I dont know how to solve it, but I think i need a token value in a params - but how to put the value there? Thank you!

Aucun commentaire:

Enregistrer un commentaire