Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thijsnado opened this issue Jun 18, 2014 · 12 comments

Comments

@thijsnado
Copy link

Getting this error even when I set:

ActiveModel::Serializer.default_host_options[:host] = '127.0.0.1:3000'

while running tests

@thijsnado
Copy link
Author

Note, this is because I'm using a url method, such as some_url from routes in my serializer.

@steveklabnik
Copy link
Contributor

This is certainly a bug. Which version of AMS are you using?

@prsimp
Copy link

prsimp commented Aug 22, 2014

After jumping from Rails 4.1.5 to 4.2.0.beta1 we started seeing this as well on AMS 0.8.1. Still digging around to see what might be causing the issue. We see it when a serializer is being used outside of a request. An example of a spec that would fail:

# Bug(id: integer, name: string, created_at: datetime, updated_at: datetime)
class BugSerializer < ActiveModel::Serializer
  attributes :id, :name, :url

  def url
    bug_url(id: object.id)
  end
end

class BugsController < ApplicationController
  def index
    render json: Bug.first
  end
end

class BugsControllerTest < ActionController::TestCase
  test "when the model is being serialized in a request" do
    get :index

    assert_equal 200, response.status
  end

  test "when serializing manually" do
    get :index

    # This will raise the ArgumentError
    expected = BugSerializer.new(Bug.first).to_json

    assert_equal expected, response.body
  end
end

Still digging to see what the issue is. This attempt to work-around fails: bug_url(id: object.id, only_path: Rails.env.test?) as the Rails still uses the FULL strategy in ActionDispatch::Routing::RouteSet#url_for which bypasses the option entirely (via ActionDispatch::Http::URL.full_url_for(options))

I'll update if I have time to dig some more -- hope the additional info helps!

@steveklabnik
Copy link
Contributor

Thanks!

@seku
Copy link

seku commented Oct 22, 2014

Rails 4.1.6, AMS 8.2.
render json: {key: SomeSerializer.new(some_object, root: false) } => the same issue
render json: some_obejct, serializer: SomeSerializer => no issue
workaround:
render json: {key: SomeSerializer.build_json(self, some_obejcet, root:false) } => no issue

@dzoltok
Copy link

dzoltok commented Mar 9, 2015

Issue still seems to be occurring on Rails 4.2.0, AMS 0.9.3, but for me it's occurring in a regular controller. Using the workaround above results in an error "undefined method `build_json' for SomeSerializer:class"

@gwagener
Copy link

I think upgrading to 0.8.3 fixed this for us with Rails 4.2, but code has changed so I can't check anymore. Can someone verify?

@typeoneerror
Copy link

Still exists in 0.9.x branch (though it looks more like a Rails issue than AMS). Looks like if I have a database field that ends with _url it's trying to look up a route url in the app's namespace. Removing "terms_url" and "privacy_url" from my serializer fixes the bug.

EDIT

Yep, I had routes called "terms_url" and "privacy_url" so looks like there was some kind of clash there. Wonder if there's a Rails way to enforce that app urls are defined outside of a global namespace? That's pretty nasty the way it is.

@bf4
Copy link
Member

bf4 commented Nov 20, 2015

@typeoneerror so, you're good? perhaps set a key for the _url attributes?

@typeoneerror
Copy link

@bf4 I'm good. What do you mean by "set a key for the _url attributes?"

@chrise86
Copy link

This also seems to be an issue on 0.10.0.rc3

@remear
Copy link
Member

remear commented Mar 17, 2016

Rails url_helpers were added to 0.10 by #1550.

@remear remear closed this as completed Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants