ruby-on-rails - 在 ActionController::Base 中替代 url_for

标签 ruby-on-rails ruby

我正在尝试将 url 传递给后台进程,以便它可以将其发布到 facebook/tweet 上。

class Weeklychecks < ActionController::Base
.
.
.
   def self.verifications
   .
   .
   .
   @my_url = url_for(:action => 'show', :controller => 'profiles', :only_path => false)
   .
   end
end

但似乎这段代码已被弃用:http://apidock.com/rails/ActionController/Base/url_for

我在基类是ApplicationController时使用过这个方法,但是我需要在这个中使用ActionController::Base。我试过添加 包括 ActionView::Helpers::UrlHelper 但它也不起作用。有解决办法吗?

我正在运行 Rails 3.2.3

谢谢

最佳答案

首先 url_for 没有被弃用。当代码被重新组织时,apidock 有时会将一个方法不再位于它原来所在的位置这一事实解释为该方法实际上已被删除。

url_for 和所有辅助方法(例如 profile_url)都作为实例方法包含在内,这就是为什么您不能从类方法中调用它们的原因。

你应该可以做到

Rails.application.routes.url_helpers.url_for(...)

无论你身在何处。 profile_path 等助手也住在这里。如果您要生成绝对网址,则需要传入 tr 主机。通常这是从 Controller 正在处理的请求的主机中获取的,但在这种情况下这显然是不可能的。

关于ruby-on-rails - 在 ActionController::Base 中替代 url_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092882/

相关文章:

ruby-on-rails - 无法将数组存储在 postgresql (rails) 的 json 字段中无法将数组转换为 json

ruby-on-rails - Ruby RestClient.post 用于登录表单

ruby - 如何在 ruby​​ 中创建界面?

ruby-on-rails - 如何在 Rails 中断言选择一个类?

python - Python 和 Perl 中是否有像 Ruby 中那样的全局对象?

ruby - 如何将 google 静态 map 保存为图像,而不仅仅是 googles api 的 URL

ruby-on-rails - Rails、gmaps4rails 不适用于 https

ruby-on-rails - Rails.cache.fetch 不缓存对象

mysql - ActiveRecord::StatementInvalid: Mysql::Error: 不正确的字符串值: '\xE2\x98\xBA.\x0D\x0A...'

ruby-on-rails - 如何使用 Rails 中的 DELETE 方法创建/注销路由/登录