Search code examples
ruby-on-railsruby-on-rails-3actioncontroller

How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis


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.

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

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

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

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

development.rb

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

And I even tried it this way:

development.rb

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

EDIT:

I've now also followed this and still the same error guide http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

application 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

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


Solution

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

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

    :)