gpt4 book ai didi

ruby-on-rails - 如何设置 config.action_controller.default_url_options = { :host = '#' '} on per environment basis

转载 作者:行者123 更新时间:2023-12-02 22:10:53 30 4
gpt4 key购买 nike

现在我正在使用它,它适用于开发主机,但当我转向生产时,我必须手动更改 {: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]”错误

开发.rb

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

我什至尝试过这种方式:

开发.rb

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

编辑:

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

应用程序 Controller

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

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

最佳答案

好吧,我发现正确的写法是

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

:)

关于ruby-on-rails - 如何设置 config.action_controller.default_url_options = { :host = '#' '} on per environment basis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5691727/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com