采取以下代码:
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
模型知道传递给它的参数哈希是否被认为是不好的做法?如果是这样,您将如何重构以使其遵循“最佳实践”?