开发者

Use a Rails helper in a model [duplicate]

开发者 https://www.devze.com 2023-03-22 23:40 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Access a view helper for a model in rails
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Access a view helper for a model in rails

I know this is probably not something I should do often, but I want to use a helper within a model.

I'm generati开发者_高级运维ng a spreadsheet in a Report model and would like to use the time_ago_in_words method in ActionView::Helpers::DateHelper

Is this doable, or should I be generating it in a view?


You can just include the module in the model, and use it like you would in a view

class Report
  include ActionView::Helpers::DateHelper
  ...
end


class Post < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

  def my_test_method(a, include_seconds = false)
    time_ago_in_words(a, include_second)
  end

 end
0

精彩评论

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