L
L
lavezzi12015-08-17 22:44:05
Ruby on Rails
lavezzi1, 2015-08-17 22:44:05

How to correctly create percentage progress for something?

Let's say you need to calculate how many pages were read as a percentage and display it as a progress bar.
My thoughts:
1. Add a progress:integer and pages:integer column to the Book.rb model
2. Let's say we create a book and write 700 pages.
3. Display the progress in the view like this:

<% @progress = @user.progress.count/pages.count * 100 %>

Please tell me how to implement it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikiforov, 2015-08-17
@lavezzi1

Book.rb

def percentage
  (progress.to_f / pages * 100).round
end

In case User has_one :book
In case User has_many :books
Instead books.first, you need the right condition by which to select a particular book.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question