开发者

question on my helper method using self

开发者 https://www.devze.com 2023-02-08 06:28 出处:网络
I created this helper method. In my view I call i开发者_如何学JAVAt with days_left(duedate). I dont really like my helper. Is it possible to use it with self. Since I dont really know how self is bein

I created this helper method. In my view I call i开发者_如何学JAVAt with days_left(duedate). I dont really like my helper. Is it possible to use it with self. Since I dont really know how self is being used. Is it the same as this in java or javascript? What object is it related to? Feel free to tune this method. Thx for your time!

def days_left(duedate)
  (if duedate.date == Date.today
    "Today"
  elsif duedate.date-Date.today < 1
    "expired"
  elsif duedate.date-Date.today == 1
    "Tomorrow"
  else
    "#{(duedate.date-Date.today).to_i}"
  end).to_s.html_safe
end


You might try moving this method to your model.

This would be similar to adding a 'full_name' method to a model with the attributes 'first_name' and 'last_name.' You wouldn't store 'full_name' separately in your database, because that would result in redundant, denormalized data.

For example:

class Employee < ActiveRecord::Base
    def full_name
        "#{first_name} #{last_name}"
    end
end

So you could similarly add the 'days_left' method to your model, which fits there because it's adding a friendlier version of an existing data attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消