开发者

Rails 3 can't get beginning_of_week

Have a publish_on datetime field.

Just trying to get to the begining_of_week from the publish_on.

Tried a helper

def start_week(publish_on)
  DateTime.parse(publish_on).beginning_of_week
end

and in view <%= start_week(@survey.publish_on) %>

Tried in my model

def set_start_week
  publish_on.beginning_of_week
end

Hell, even tried this helper

def this_should_work
  DateTime.now.beginning_of_week
end

But everything returns a invalid date to my view. It works in irb, why not in my view?

EDIT

module SurveysHelper
  require 'date'
  require 'time'

  def this_should_work
    DateTime.now.beginning_of_week
  end
end

take_survey.h开发者_开发问答tml.erb

<%= this_should_work %>

Error invalid date


I encountered the same problem when trying to use ActiveSupport outside of Rails. I found the answer here:

http://guides.rubyonrails.org/active_support_core_extensions.html

Here is the key bit:

require 'active_support/all'

Have you customized your rails stack to no longer include ActiveSupport?


In rails 3.0.9 I got

undefined method `at_beginning_of_week'

wherever I put it, my alternative solution is:

def SomeMethodPutAnywhere
...
@datey = Date.today
@monday = @datey - @datey.wday + 1

#@monday is now the monday of the current week.
#Remove the constant '+1' if your week begins sunday...
...
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜