所以在我的ClientsController.rb
我有一个之前的过滤器,它执行以下操作:
检查当前年份和月份。如果月份在六月之后,则将变量设置vote_year
为明年。如果不是,则将其设置vote_year
为今年。
然后我根据那一年设置日期属性,以及 7 月 1 日的艰难月份和日期。
基本上,这个特定的日期是每年的 7 月 1 日。我希望这个前置过滤器根据运行此过滤器的时间是在 7 月 1 日之前还是之后来设置下一个日期。
我的代码如下:
before_filter :set_next_vote_date, :only => [:create, :new, :edit, :update]
private
def set_next_vote_date
client = current_user.clients.find(params[:id])
today = DateTime.parse(Time.now.to_s)
year = today.year
if today.month == 7 && today.day == 1
vote_year = today.year
elsif today.month > 6
vote_year = year + 1
else
vote_year = year
end
client.next_vote = "#{vote_year}-07-01"
end
问题是,每当我在这些控制器上执行任何这些操作时,都不会引发错误。但是,next_vote
客户记录上的属性没有被更新。
我错过了什么?
编辑1:
使用update_attribute
(不带!
)后,我没有收到错误消息,但我没有在日志中看到此特定属性正在更新。
Started PUT "/clients/1" for 127.0.0.1 at 2012-09-08 20:09:17 -0500
Processing by ClientsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQc5N=", "client"=>{"name"=>"John F Kennedy", "email"=>"jfk@email.com", "phone"=>"8234698765", "firm_id"=>"1", "topic_ids"=>"2"}, "commit"=>"Update Client", "id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Client Load (0.1ms) SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
(0.0ms) commit transaction
CACHE (0.0ms) SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
Topic Load (0.6ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1 [["id", 2]]
Topic Load (0.2ms) SELECT "topics".* FROM "topics" INNER JOIN "clients_topics" ON "topics"."id" = "clients_topics"."topic_id" WHERE "clients_topics"."client_id" = 1
(0.4ms) UPDATE "clients" SET "phone" = 823498765, "updated_at" = '2012-09-09 01:09:17.631839' WHERE "clients"."id" = 1
(1.4ms) commit transaction
Redirected to http://localhost:3000/clients/1
请注意,该next_vote
属性未更新。当然,我没有在edit
部分表单中包含该属性,但我假设这before_filter
- 如果它正在执行,将更新记录。但我什至不确定它是否正在执行。
编辑2:
没关系,它现在似乎正在工作。上面的日志粘贴是在编辑操作之后,而 before_filter 在编辑操作之前执行 - DUH!傻我:)