vendredi 17 juillet 2020

Validation to check if algorithem value is correct with input value from user before saving

**when the user click "create new task" , the field "idd" will excetu algorithem and equality with the value by user i.e 202078(2+2+8-7=5), the user should input 5 to create the new task or the task not create **

def show
     @ruby = Task.where(id: params[:id])
     @yearss = @ruby.first.created_at.year.to_s.split(//)
     @yearsss = @ruby.first.created_at.year.to_s
     @monthss = @ruby.first.created_at.month.to_s.split(//)
     @monthsss = @ruby.first.created_at.month.to_s
     @number_id = @ruby.first.id.to_s.split(//)
     @number_ids = @ruby.first.id.to_s
     @iddd = @ruby.first.idd
     @both_odd = (@yearss + @monthss + @number_id).map(&:to_i).select(&:odd?).sum
     @both_even = (@yearss + @monthss + @number_id).map(&:to_i).select(&:even?).sum
     @iddd = (@both_even - @both_odd).abs
     if @iddd  > 9
       (@iddd %= 10).to_s
     else
       @iddd.to_s
     end
     @pardon = (@yearsss + @monthsss + @number_ids + @iddd.to_s)
     @rails = @ruby.first.lname.to_s.slice(0,2) + @ruby.first.name.to_s.slice(0,2) + @pardon
  end
  def new
    @task = Task.new
  end

  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

model

class Task < ApplicationRecord
    validates :name, :lname ,presence: true
end

Aucun commentaire:

Enregistrer un commentaire