相关疑难解决方法(0)

如何在每个环境的基础上设置config.action_controller.default_url_options = {:host ='#''}

现在我正在使用它适用于开发主机,但是当我转向生产时,我必须手动更改{: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
Run Code Online (Sandbox Code Playgroud)

我想使用它然后定义每个环境的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
Run Code Online (Sandbox Code Playgroud)

我已经尝试将此添加到我的config/environments/development.rb但我仍然得到"缺少主机链接到!请提供:host参数或设置default_url_options [:host]"错误

development.rb

config.action_controller.default_url_options = {:host => "localhost:3000"}
Run Code Online (Sandbox Code Playgroud)

我甚至用这种方式尝试过:

development.rb

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)

这让我发疯,我在这里失踪了什么?

ruby-on-rails actioncontroller ruby-on-rails-3

58
推荐指数
5
解决办法
5万
查看次数