看看下面的代碼:模型意識到params hash - Rails反模式?
class ChallengesController < ApplicationController
def update
@challenge = Challenge.find(params[:id])
@challenge.update!(params[:challenge]) # never an expected error, show error page and give hoptoad notification
respond_to do |format|
format.html { redirect_to :action => 'index' }
end
end
end
class Challenge < ActiveRecord::Base
def update!(options)
if options[:accept] == '1' then
self.accepted = true
self.response_at = Time.now
self.shots = options[:shots] unless options[:shots].blank?
self.challengee_msg = options[:challengee_msg] unless options[:challengee_msg].blank?
else
self.accepted = false
self.response_at = Time.now
end
end
end
是它認爲不好的做法爲模型要注意params哈希表被傳遞給它?如果是這樣,你會如何重構,以便遵循「最佳實踐」?
要明確,你不需要實現update_attributes - 該方法已經存在了你。 –