现在我正在使用它适用于开发主机,但是当我转向生产时,我必须手动更改{: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)
这让我发疯,我在这里失踪了什么?
我正在开发一个混合了http和https的网站 - 最简单/最简单的方法是让链接使用正确的路由协议 - 是否可以在路由文件中指定?
假设我在Rails 3中有以下路由.
match "/test" => "test#index", :as => :test, :constraints => { :protocol => 'https' }
如果我在http页面上,并且我使用test_url()它,它将输出http://domain.com/test.我想改为https://domain.com/test.
我知道我可以使用test_url(:secure => true),但那是重复的逻辑.
我知道我可以http://domain.com/test到https://domain.com/test,但这是一个额外的重定向,加上它在表单帖子上失败了.
想法?