目前我正在使用这个方法,它适用于开发主机,但当我切换到生产环境时,我必须手动更改{:host => ""}代码。
post.rb
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
我想使用这个功能,并根据环境定义 default_url_options:
post.rb
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
我尝试将以下内容添加到我的config/environments/development.rb文件中,但仍然出现“缺少链接的主机!请提供:host参数或设置default_url_options [:host]”错误。
development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
我甚至尝试过这种方式:
development.rb
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}
编辑:
我现在也按照此操作,但仍然遇到相同的错误,请参考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
这让我疯狂,我错过了什么???
routes.rb
的顶部:Example::Application.routes.default_url_options[:host] = 'example.com'
。 - lime