如何从模型的类方法访问 UrlWriter url/路径生成器?
我想从模型的类方法生成 url。我之前已经通过实例方法通过简单地包含 ActionController::UrlWriter
来完成此操作 - 我尝试将其包含在实例定义范围和类定义范围中,但无济于事。
class Foo < ActiveRecord::Base
# only works for instance methods
# include ActionController::UrlWriter
class << self
# results in this error: undefined method `default_url_options' for Class:Class
# include ActionController::UrlWriter
def my_method
return user_sprockets_url(:thingy => 'blue')
end
end
end
I want to generate urls from a model's class method. I've done this before from an instance method by simply including ActionController::UrlWriter
-- I tried including this in the instance definition scope and also the class definition scope, to no avail.
class Foo < ActiveRecord::Base
# only works for instance methods
# include ActionController::UrlWriter
class << self
# results in this error: undefined method `default_url_options' for Class:Class
# include ActionController::UrlWriter
def my_method
return user_sprockets_url(:thingy => 'blue')
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
甜的!
一点重构..
用法...
Sweet!
a little refactoring..
usage...