Rails: Missing host to link to! Please provide :host parameter or set default_url_options[:host]

前端 未结 15 2576
深忆病人
深忆病人 2020-11-27 10:34

I have been googling for about 90 minutes now and still don\'t have an answer to this. Where do I set default_url_options? I\'ve already set it for conf

相关标签:
15条回答
  • 2020-11-27 11:14

    You can set default url options in the Application Controller:

    class ApplicationController < ActionController::Base
      def default_url_options
        {:locale => I18n.locale}
      end
    end
    

    http://guides.rubyonrails.org/action_controller_overview.html#default_url_options

    0 讨论(0)
  • 2020-11-27 11:17

    go to config/environments/test.rb

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

    0 讨论(0)
  • 2020-11-27 11:18

    You need to add the following line at every environment:

    config.action_mailer.default_url_options = { :host => "yourhost" }

    That way, it can work in all environments and could be different from environment to environment. For example:

    development.rb

    config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }
    

    test.rb

    config.action_mailer.default_url_options = { :host => "test.yourhost.com" }
    

    production.rb

    config.action_mailer.default_url_options = { :host => "www.yourhost.com" }
    
    0 讨论(0)
  • 2020-11-27 11:18

    I solved the issue by configuring environment.rb as

    YourApp::Application.default_url_options = YourApp::Application.config.action_mailer.default_url_options

    You need to set default_url_options for action mailer against each environment like development, testing, staging and production etc.

    Reference: Missing host to link to! Please provide :host parameter or set default_url_options[:host]

    0 讨论(0)
  • 2020-11-27 11:23

    Funny thing, that setting config.action_mailer.default_url_options does not help for me. Also, messing around with environment-independent settings in places I felt like it does not belong was not satisfying for me. Additionally, I wanted a solution that worked when generating urls in sidekiq/resque workers.

    My approach so far, which goes into config/environments/{development, production}.rb:

    MyApp::Application.configure do
        # Stuff omitted...
    
        config.action_mailer.default_url_options = {
          # Set things here as usual
        }
    end
    
    MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options
    

    This works for me in rails >= 3.2.x.

    0 讨论(0)
  • 2020-11-27 11:24
    Your::Application.routes.draw do
      default_url_options :host => "example.com"
    
      # ... snip ...
    end
    

    Somewhere in routes.rb :)

    0 讨论(0)
提交回复
热议问题