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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With