我们的 Location 模型有一个自定义操作 (:register)。支持代码与标准的 :update 非常相似。由于inherited_resources 为我们提供了“模板”,我们从actions.rb 中复制了更新代码,将“update_attributes”更改为“register”,flash 消息反映了不同的操作。这感觉不是很干燥。我们想使用:update 代替。有任何想法吗?
class LocationsController < InheritedResources::Base
def register(options={}, &block)
#TODO: copied update from actions.rb. I expect there is a better way.
# All I changed was the flash message (to reflect the action)
# and the method call on the object (update_attributes -> register)
object = resource
if object.register
set_flash_message!(:notice, '{{resource_name}} was successfully registered.')
options[:location] ||= resource_url rescue nil
respond_with_dual_blocks(object, options, true, block)
else
set_flash_message!(:error)
respond_with_dual_blocks(object, options, false, block)
end
end