如何在每个环境的基础上设置 config.action_controller.default_url_options = {:host = '#''} [英] How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis

查看:44
本文介绍了如何在每个环境的基础上设置 config.action_controller.default_url_options = {:host = '#''}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用它,它适用于开发主机,但是当我进入生产环境时,我必须手动更改 {:host => ""} 代码.

Right now I'm using this which works for the development host, but I have to manually change the {:host => ""} code when I move to production.

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:

I'd like to use this and then define the default_url_options per environment:

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]"错误

I've tried adding this to my config/environments/development.rb but I still get the "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" error

config.action_controller.default_url_options = {:host => "localhost:3000"}

我什至这样试过:

config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}

我现在也遵循了这个并且仍然是相同的错误指南 http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

I've now also followed this and still the same error guide 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

这让我发疯,我在这里错过了什么???

This is driving me crazy, what am I missing here???

推荐答案

好吧,我想出了正确的写法

Okay I figured it out the correct way to write it is

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

:)

这篇关于如何在每个环境的基础上设置 config.action_controller.default_url_options = {:host = '#''}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆