现在我正在使用它适用于开发主机,但是当我转向生产时,我必须手动更改{:host =>""}代码.
def share_all
url = Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
if user.authentications.where(:provider => 'twitter').any?
user.twitter_share(url)
end
end
Run Code Online (Sandbox Code Playgroud)
我想使用它然后定义每个环境的default_url_options:
def share_all
url = Rails.application.routes.url_helpers.post_url(self)
if user.authentications.where(:provider => 'twitter').any?
user.twitter_share(url)
end
end
Run Code Online (Sandbox Code Playgroud)
我已经尝试将此添加到我的config/environments/development.rb但我仍然得到"缺少主机链接到!请提供:host参数或设置default_url_options [:host]"错误
config.action_controller.default_url_options = {:host => "localhost:3000"}
Run Code Online (Sandbox Code Playgroud)
我甚至用这种方式尝试过:
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}
Run Code Online (Sandbox Code Playgroud)
编辑:
我现在也遵循了这个并且仍然是相同的错误指南http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
class ApplicationController < ActionController::Base
protect_from_forgery
include ApplicationHelper
def default_url_options
if Rails.env.production?
{ :host => "example.com"}
else
{:host => "example1.com"}
end
end
end
Run Code Online (Sandbox Code Playgroud)
这让我发疯,我在这里失踪了什么?