Reputation: 3514
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.
Upvotes: 7
Views: 5031
Reputation: 7614
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).
Upvotes: 3
Reputation: 5914
This error generally causes when we try to user any url helpers in places where it is not supposed to use. For example if I try to generate password record url from mailer class action, using reset_password_url(@user)
, i gets same error.
Please make sure that you are not using any _path or _url methods within your model class. Moreover I don't think its a best practice to user url_helpers inside the model.
Upvotes: -2