Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include Rails.application.routes.url_helpers cause ArgumentError Missing host to link to

I'm developing Rails 3.1.1.
Including url_helpers in Model cause an ArgumentError on saving model.

class Medium < ActiveRecord::Base
  include Rails.application.routes.url_helpers

  .
  .
end

class MediaController < ApplicationController

  def create
    @medium = Medium.new(params[:medium])
    @media.save # => cause ArgumentError
  end
end

ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

Another model which also include url_helper doesn't cause error.
What's wrong?
Thanks in advance.

like image 824
DIGITALSQUAD Avatar asked Nov 03 '11 12:11

DIGITALSQUAD


1 Answers

You need to pass the host as an argument in the call where you are using the helper:

Rails.application.routes.url_helpers.media_url(:host => "localhost:3000")

or a different route like this:

Rails.application.routes.url_helpers.media_url(self, :host => AppConfig.host)

where AppConfig.host is the host depending on environemnt (localhost:300 for development env).

like image 168
amit_saxena Avatar answered Oct 02 '22 22:10

amit_saxena