命名路由我有一些命名路由這樣rake routes
:訪問的Rake文件
在Rake文件我只是想能夠調用birthdays_url
就像我會在我的觀點。
task :import_birthdays => :environment do
url = birthdays_url
end
但我發現了一個錯誤undefined local variable or method 'birthdays_url' for main:Object
命名路由我有一些命名路由這樣rake routes
:訪問的Rake文件
在Rake文件我只是想能夠調用birthdays_url
就像我會在我的觀點。
task :import_birthdays => :environment do
url = birthdays_url
end
但我發現了一個錯誤undefined local variable or method 'birthdays_url' for main:Object
您可以在使用此示例代碼你的耙子任務:
include Rails.application.routes.url_helpers
puts birthdays_url(:host => 'example.com')
,或者你可以在你的耙子任務中使用此示例代碼:
puts Rails.application.routes.url_helpers.birthdays_url(:host => 'example.com')
如果你只是想在URL的路徑部分,你可以使用(:only_path => true)
而不是(:host => 'example.com')
。所以,那隻會給你/birthdays
而不是http://example.com/birthdays
。
你需要無論是(:host => 'example.com')
或(:only_path => true)
片,因爲耙任務不知道的信息位和將給這個錯誤沒有它:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
使用本:
Rails.application.routes.url_helpers.birthdays_url
還是要更簡潔:
include Rails.application.routes.url_helpers
url = birthdays_url
爲Rails 4包含的代碼與您在域你的耙子任務的頂部
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
我不喜歡硬編碼的主機。所以我使用'host:YOUR-APP-NAME :: Application.config.action_mailer.default_url_options [:host]''''''''''''''''' – user2726983