我想重写中的资源的方法就像在这个问题解释说:Remove .xml extension from ActiveResource request这一个: i want to use a REST api, i cannot manage to set active resource to use it无法覆盖/猴补丁与导轨一个Rails 3.1.3方法
为此我测试:
在我的应用程序名为active_resource.rb用下面的代码文件/配置/在itializers /文件夹中创建:
class ActiveResource::Base
def element_path(id, prefix_options = {},query_options = nil)
check_prefix_options(prefix_options)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"
end
end
添加我的模型里面的方法。这里是我的模型代码:
class Player < ActiveResource::Base
def element_path(id, prefix_options = {}, query_options = nil)
check_prefix_options(prefix_options)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"
end
self.site = "http://ws.maniaplanet.com/"
self.user="**********"
self.password="*********"
end
为了验证我的自定义代码压倒一切我曾尝试使用
puts "called this method"
或
ActionController::Base.logger.info "called this method"
它从来没有工作过。
为什么我不能重写rails方法元素路径?
UPDATE
试图把active_resource.rb额外取消注释在application.rb中的config.autoload_paths += %W(#{config.root}/extras)
行之后。没有变化
如果我把base.rb文件与我的类和方法在lib/active_resource /它破坏我的应用程序。我无法启动轨道服务器了
如果您在重写它之前尝试要求ActiveResource,该怎么办? 'require“active_resource”'。它会有帮助吗? – cutalion 2012-03-11 20:26:52
没有帮助:/ – Syl 2012-03-11 23:31:02