尝试在模型中包含 ActionController::UrlWriter 时未初始化的内容 [英] Uninitialized content when trying to include ActionController::UrlWriter in model

查看:65
本文介绍了尝试在模型中包含 ActionController::UrlWriter 时未初始化的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails 3 beta 4 并试图在我的模型中包含 ActionController::UrlWriter,据我所知,这是正确的方法,但我得到未初始化的常量 ActionController::UrlWriter".

I'm using Rails 3 beta 4 and trying to include ActionController::UrlWriter in my model, which is the correct way to go about it as far as i can tell, but i get "Uninitialized Constant ActionController::UrlWriter".

知道为什么会这样吗?它在轨道 3 中移动了吗?

Any idea why that would be? Did it move in rails 3?

推荐答案

首先我同意 zed.这不应该在模型中完成.您的模型应该不知道任何 http 网址.

First I agree with zed. This shouldn't be done in a model. Your models should be unaware of any http url.

我在resque工作中做同样的事情.这就是我所做的:

I do the same in a resque job. Here's what I do :

include ActionDispatch::Routing::UrlFor
include ActionController::PolymorphicRoutes
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'

然后你可以调用任何常用的 url 生成器.

Then you can call any usual url generator.

url_for(object)
page_url(object)

它将在定义为 default_url_options[:host]

这篇关于尝试在模型中包含 ActionController::UrlWriter 时未初始化的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆