所以在我的ClientsController.rb
我有一個之前的篩選器,執行以下操作:爲什麼這個before_filter不能執行?
檢查當前年份和月份。如果月份在六月以後,則將變量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"=>"[email protected]", "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!傻我:)
在我看來,你缺少'client.save'。除非在控制器操作中稍後調用它。 你確定這段代碼沒有被執行,還是隻是記錄屬性沒有被更新? – gregates