简体   繁体   中英

calculate number in views ruby on rails

I would like to calculate mid test and final test mark into a grade. Eg:

mid test = 80 , final test = 80
(midtest + finaltest)/2 >=80
grade = "A"

Is it possible to do an if condition in views and insert into database? Something like:

 if (midtest + finaltest) / 2 >= 80
      grade = "A"
    elsif (midtest + finaltest)/2 >= 70 and < 80
      grade = "B"
    elsif (midtest + finaltest) /2 >= 60 and < 70
      grade = "C"

So that in views we don't need a text_field for grades and so that the calculation is automatically inserted into the database.





def create

    @nilai = Nilai.new(params[:nilai])
    @nilai.get_grade
    respond_to do |format|
      if @nilai.save
        format.html { redirect_to @nilai, notice: 'Nilai was successfully created.' }
        format.json { render json: @nilai, status: :created, location: @nilai }
      else
        format.html { render action: "new" }
        format.json { render json: @nilai.errors, status: :unprocessable_entity }
      end
    end

  end

Model

class Nilai < ActiveRecord::Base

  attr_accessible :grade, :id_makul, :id_mhs, :id_nilai, :uas, :uts

  def get_grade

    @calculate = (self.uas + self.uts)/2
    if @calculate >= 80
      self.grade = "A"
      elsif @calculate >=70 and @calculate < 80
        self.grade = "B"
      elsif @calculate >=60 and @calculate <70
        self.grade = "C"
      elsif @calculate >=50 and @calculate <60
        self.grade = "D"
    else
      self.grade = "E"
    end  
  end

end

Still guessing what you really want and why you think you have to do it in the view...

As I said above, Views should be used solely for code that displays data that already exists. Code that inserts things into the database is for your models and controllers.

I suggest either:

1) you create a method on your model called "grade" eg:

def grade
  if (midtest + finaltest) / 2 >= 80
    return "A"
  elsif (midtest + finaltest)/2 >= 70 and < 80
    return "B"
  elsif (midtest + finaltest) /2 >= 60 and < 70
    return "C"
  else
    return "F"
  end
end

now, you can call this method from your view eg:

Grade: <%=  @my_model.grade %>

Note that this method does not insert it into the database.

OR

2) you create a method as above on a before_save callback

eg lets say you're storing it into the "grade" column in the db:

class MyModel < ActiveRecord::Base

  before_create :calculate_grade

  def calculate_grade
    if (midtest + finaltest) / 2 >= 80
      self.grade = "A"
    elsif (midtest + finaltest)/2 >= 70 and < 80
      self.grade = "B"
    elsif (midtest + finaltest) /2 >= 60 and < 70
      self.grade = "C"
    else
      self.grade = "F"
    end
  end
end

And now whenever your model gets saved, the grade gets re-calculated from the test scores and saved into the db alongside. so you can use "grade" in your views as above, but it's coming from the database column

Grade: <%=  @my_model.grade %>

technically you could do almost anything in views, after all view is also a ruby file. So you could have conditions, DB connections etc..

BUT, Its not a good practise to have your logic in your views. Always try to have your view to display only. All the processing logic should be in Models

Idea is, Fat models, thin controllers are views are only for presentation. So in your case try and see at least to get your login in to a helper method.

One more thing I notice, you could have this line

 (midtest + finaltest) / 2

to

 average_marks = (midtest + finaltest) / 2 

and use average_marks , in other places, as its more DRY (Dont Repeat Yourself)

HTH :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM