rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

Undefined method controller using links #2083

Closed cjmarkham closed 7 years ago

cjmarkham commented 7 years ago

Expected behavior vs actual behavior

Expected links to be returned Actual:

undefined local variable or method `controller' for # Did you mean? console

Steps to reproduce

Set adapter to json_api and add relationship links to serializer

Environment

ActiveModelSerializers Version (commit ref if not on tag):

active_model_serializers-0.10.4

Output of ruby -e "puts RUBY_DESCRIPTION":

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

Backtrace

actionview (5.0.0.1) lib/action_view/routing_url_for.rb:122:in `url_options'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:228:in `call'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:177:in `call'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:295:in `block (2 levels) in define_url_helper'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/link.rb:51:in `instance_eval'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/link.rb:51:in `initialize'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:51:in `new'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:51:in `block in links_for'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:50:in `each'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:50:in `each_with_object'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:50:in `links_for'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api/relationship.rb:22:in `as_json'
active_model_serializers (0.10.4) lib/active_model_serializers/adapter/json_api.rb:441:in `block in relationships_for'

Additonal helpful information

Controller method

def index
  render json: resource, adapter: :json_api
end

Serializer:

class Api::FooSerializer < ActiveModel::Serializer
  include Rails.application.routes.url_helpers

  link(:self) { api_foo_path(object.id) }
end
bf4 commented 7 years ago

Any particular reason you're include Rails.application.routes.url_helpers ?

cjmarkham commented 7 years ago

Without it I get errors on all routes used within the serializer:

undefined local variable or method `api_foo_path' for #

cjmarkham commented 7 years ago

Just a heads up. It works fine in development but not in production. In an attempt to replicate the error on development I set some of the configs to the same as production. Turns out when I set config.eager_load from false to true, it breaks with the above error. With it set to true I cannot use any routes in serializers (undefined local variable or method `api_foo_path') and get the controller error message using links.

bf4 commented 7 years ago

Might be a load order thing... and we need to add reload routes before including it

B mobile phone

On Mar 21, 2017, at 8:52 PM, Carl Markham notifications@github.com wrote:

Just a heads up. It works fine in development but not in production. In an attempt to replicate the error on development I set some of the configs to the same as production. Turns out when I set config.eager_load from false to true, it breaks with the above error. With it set to true I cannot use any routes in serializers (undefined local variable or method `api_foo_path') and get the controller error message using links.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

cjmarkham commented 7 years ago

I'm going to close this for now as it appears to be a config issue. I created a new rails app and followed the same steps but couldn't reproduce.

If I find the issue I will come back to let you know.