active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

url_helpers non-functional in links method?

Open michael-reeves opened this issue 8 years ago • 26 comments

Expected behavior vs actual behavior

I was trying to implement the links behavior described in how_to/add_relationship_links.md

When I implement def links as described I get undefined method 'api_v1_entity_url'

Same error when using api_v1_entity_path

Steps to reproduce

My serializer example:

class IpAddressSerializer < ActiveModel::Serializer
  # type :ip_address

  attributes :id, :address, :gateway, :netmask, :ip_use, :ip_type, :links

  def links
    {
      entity: api_v1_entity_url(object.entity.name)
    }
  end
  # link(:entity) { api_v1_entity_url(object.entity.name) }
  

  def ip_use
    object.ip_use.name
  end

  def ip_type
    object.ip_type.name
  end
end

If I remove the links attr and uncomment link(:entity) piece, I get correct response.

{
    "data": {
        "id": "1",
        "type": "ip-addresses",
        "attributes": {
            "address": "192.168.10.10",
            "gateway": "192.168.10.1",
            "netmask": "24",
            "ip-use": "IPMI",
            "ip-type": "IPv4"
        },
        "links": {
             "entity": "http://localhost/api/v1/entities/server1"
        }
    }
}

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.2

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]

OS Type & Version: OS X 10.11.6

Integrated application and version (e.g., Rails, Grape, etc): Rails 4.2.6

Backtrace

Completed 500 Internal Server Error in 51ms (ActiveRecord: 1.4ms)

NoMethodError - undefined method `api_v1_entity_url' for #<IpAddressSerializer:0x007fe3028e40a8>:
  app/serializers/ip_address_serializer.rb:8:in `links'
  active_model_serializers (0.10.2) lib/active_model/serializer.rb:195:in `read_attribute_for_serialization'
  active_model_serializers (0.10.2) lib/active_model/serializer/field.rb:23:in `value'
  active_model_serializers (0.10.2) lib/active_model/serializer/attributes.rb:22:in `block in attributes'
  active_model_serializers (0.10.2) lib/active_model/serializer/attributes.rb:19:in `attributes'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:292:in `attributes_for'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:301:in `block in resource_object_for'
  active_model_serializers (0.10.2) lib/active_model/serializer/caching.rb:226:in `fetch'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:297:in `resource_object_for'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:248:in `process_resource'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:238:in `block in resource_objects_for'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:238:in `resource_objects_for'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:90:in `success_document'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/json_api.rb:59:in `serializable_hash'
  active_model_serializers (0.10.2) lib/active_model_serializers/adapter/base.rb:59:in `as_json'
  activesupport (4.2.6) lib/active_support/json/encoding.rb:35:in `encode'
  activesupport (4.2.6) lib/active_support/json/encoding.rb:22:in `encode'
  activesupport (4.2.6) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
  active_model_serializers (0.10.2) lib/active_model_serializers/serializable_resource.rb:8:in `to_json'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:69:in `block (3 levels) in notify'
  activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
  activesupport (4.2.6) lib/active_support/callbacks.rb:343:in `block (2 levels) in simple'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:22:in `block (3 levels) in instrument_rendering'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:79:in `block in notify_render'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:78:in `notify_render'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:21:in `block (2 levels) in instrument_rendering'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:95:in `block in tag_logger'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:95:in `tag_logger'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:20:in `block in instrument_rendering'
  activesupport (4.2.6) lib/active_support/callbacks.rb:441:in `block in make_lambda'
  activesupport (4.2.6) lib/active_support/callbacks.rb:342:in `block in simple'
  activesupport (4.2.6) lib/active_support/callbacks.rb:497:in `block in around'
  activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_render_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  active_model_serializers (0.10.2) lib/active_model_serializers/logging.rb:68:in `block (2 levels) in notify'
  actionpack (4.2.6) lib/action_controller/metal/renderers.rb:116:in `block in <module:Renderers>'
  active_model_serializers (0.10.2) lib/action_controller/serialization.rb:53:in `block (2 levels) in <module:Serialization>'
  actionpack (4.2.6) lib/action_controller/metal/renderers.rb:45:in `block in _render_to_body_with_renderer'
  /Users/mikal/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/set.rb:306:in `each'
  actionpack (4.2.6) lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
  actionpack (4.2.6) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
  actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
  activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/mikal/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
  activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:43:in `render'
  responders (2.3.0) lib/action_controller/responder.rb:260:in `display'
  responders (2.3.0) lib/action_controller/responder.rb:214:in `api_behavior'
  responders (2.3.0) lib/action_controller/responder.rb:193:in `rescue in to_format'
  responders (2.3.0) lib/action_controller/responder.rb:185:in `to_format'
  responders (2.3.0) lib/action_controller/responder.rb:163:in `respond'
  responders (2.3.0) lib/action_controller/responder.rb:156:in `call'
  responders (2.3.0) lib/action_controller/respond_with.rb:205:in `respond_with'
  app/controllers/api/v1/ip_addresses_controller.rb:7:in `show'
  actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
  meta_request (0.4.0) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  meta_request (0.4.0) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.6) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.4.0) lib/meta_request/middlewares/headers.rb:16:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.6) lib/rails/engine.rb:518:in `call'
  railties (4.2.6) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  puma (3.5.2) lib/puma/configuration.rb:225:in `call'
  puma (3.5.2) lib/puma/server.rb:569:in `handle_request'
  puma (3.5.2) lib/puma/server.rb:406:in `process_client'
  puma (3.5.2) lib/puma/server.rb:271:in `block in run'
  puma (3.5.2) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

config/initializers/active_model_serializer.rb

ActiveModelSerializers.config.adapter = :json_api

Rails.application.routes.default_url_options = {
  host: 'localhost'
}

Gemfile.lock

GIT
  remote: https://github.com/activerecord-hackery/squeel.git
  revision: 5542266d502db8022e14105f9dfb455a79d6fc4a
  specs:
    squeel (1.2.3)
      activerecord (>= 3.0)
      activesupport (>= 3.0)
      polyamorous (~> 1.1.0)

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.6)
      actionpack (= 4.2.6)
      actionview (= 4.2.6)
      activejob (= 4.2.6)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.6)
      actionview (= 4.2.6)
      activesupport (= 4.2.6)
      rack (~> 1.6)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (4.2.6)
      activesupport (= 4.2.6)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    active_model_serializers (0.10.2)
      actionpack (>= 4.1, < 6)
      activemodel (>= 4.1, < 6)
      jsonapi (~> 0.1.1.beta2)
      railties (>= 4.1, < 6)
    activejob (4.2.6)
      activesupport (= 4.2.6)
      globalid (>= 0.3.0)
    activemodel (4.2.6)
      activesupport (= 4.2.6)
      builder (~> 3.1)
    activerecord (4.2.6)
      activemodel (= 4.2.6)
      activesupport (= 4.2.6)
      arel (~> 6.0)
    activesupport (4.2.6)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    addressable (2.4.0)
    airbrussh (1.0.2)
      sshkit (>= 1.6.1, != 1.7.0)
    arel (6.0.3)
    auto_strip_attributes (2.0.6)
      activerecord (>= 3.0)
    autoprefixer-rails (6.3.7)
      execjs
    awesome_print (1.7.0)
    bcrypt (3.1.11)
    better_errors (2.1.1)
      coderay (>= 1.0.0)
      erubis (>= 2.6.6)
      rack (>= 0.9.0)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    bootstrap-kaminari-views (0.0.5)
      kaminari (>= 0.13)
      rails (>= 3.1)
    bootstrap-sass (3.3.6)
      autoprefixer-rails (>= 5.2.1)
      sass (>= 3.3.4)
    builder (3.2.2)
    byebug (9.0.5)
    callsite (0.0.11)
    capistrano (3.5.0)
      airbrussh (>= 1.0.0)
      capistrano-harrow
      i18n
      rake (>= 10.0.0)
      sshkit (>= 1.9.0)
    capistrano-bundler (1.1.4)
      capistrano (~> 3.1)
      sshkit (~> 1.2)
    capistrano-harrow (0.5.2)
    capistrano-rails (1.1.7)
      capistrano (~> 3.1)
      capistrano-bundler (~> 1.1)
    capistrano-rvm (0.1.2)
      capistrano (~> 3.0)
      sshkit (~> 1.2)
    capistrano-scm-copy (0.7.0)
      capistrano (~> 3.0)
    capybara (2.7.1)
      addressable
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    childprocess (0.5.9)
      ffi (~> 1.0, >= 1.0.11)
    coderay (1.1.1)
    coffee-rails (4.1.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.1.x)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.10.0)
    concurrent-ruby (1.0.2)
    database_cleaner (1.5.3)
    debug_inspector (0.0.2)
    devise (4.2.0)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0, < 5.1)
      responders
      warden (~> 1.2.3)
    devise_ldap_authenticatable (0.8.5)
      devise (>= 3.4.1)
      net-ldap (>= 0.6.0, <= 0.11)
    diff-lcs (1.2.5)
    docile (1.1.5)
    erubis (2.7.0)
    execjs (2.7.0)
    fabrication (2.15.2)
    ffi (1.9.14)
    git-version-bump (0.15.1)
    globalid (0.3.6)
      activesupport (>= 4.1.0)
    i18n (0.7.0)
    jbuilder (2.5.0)
      activesupport (>= 3.0.0, < 5.1)
      multi_json (~> 1.2)
    jquery-rails (4.1.1)
      rails-dom-testing (>= 1, < 3)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    json (1.8.3)
    jsonapi (0.1.1.beta6)
      jsonapi-parser (= 0.1.1.beta3)
      jsonapi-renderer (= 0.1.1.beta1)
    jsonapi-parser (0.1.1.beta3)
    jsonapi-renderer (0.1.1.beta1)
    kaminari (0.17.0)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    launchy (2.4.3)
      addressable (~> 2.3)
    loofah (2.0.3)
      nokogiri (>= 1.5.9)
    mail (2.6.4)
      mime-types (>= 1.16, < 4)
    meta_request (0.4.0)
      callsite (~> 0.0, >= 0.0.11)
      rack-contrib (~> 1.1)
      railties (>= 3.0.0, < 5.1.0)
    method_source (0.8.2)
    mime-types (3.1)
      mime-types-data (~> 3.2015)
    mime-types-data (3.2016.0521)
    mini_portile2 (2.1.0)
    minitest (5.9.1)
    multi_json (1.12.1)
    net-ldap (0.11)
    net-scp (1.2.1)
      net-ssh (>= 2.6.5)
    net-ssh (3.2.0)
    nokogiri (1.6.8.1)
      mini_portile2 (~> 2.1.0)
    orm_adapter (0.5.0)
    paper_trail (5.2.2)
      activerecord (>= 3.0, < 6.0)
      request_store (~> 1.1)
    paranoia (2.1.5)
      activerecord (~> 4.0)
    pg (0.18.4)
    polyamorous (1.1.0)
      activerecord (>= 3.0)
    pry (0.10.4)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    pry-byebug (3.4.0)
      byebug (~> 9.0)
      pry (~> 0.10)
    puma (3.5.2)
    quiet_assets (1.1.0)
      railties (>= 3.1, < 5.0)
    rack (1.6.4)
    rack-contrib (1.4.0)
      git-version-bump (~> 0.15)
      rack (~> 1.4)
    rack-test (0.6.3)
      rack (>= 1.0)
    railroady (1.5.1)
    rails (4.2.6)
      actionmailer (= 4.2.6)
      actionpack (= 4.2.6)
      actionview (= 4.2.6)
      activejob (= 4.2.6)
      activemodel (= 4.2.6)
      activerecord (= 4.2.6)
      activesupport (= 4.2.6)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.6)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.7)
      activesupport (>= 4.2.0.beta, < 5.0)
      nokogiri (~> 1.6.0)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.3)
      loofah (~> 2.0)
    railties (4.2.6)
      actionpack (= 4.2.6)
      activesupport (= 4.2.6)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (11.3.0)
    ransack (1.6.5)
      actionpack (>= 3.0)
      activerecord (>= 3.0)
      activesupport (>= 3.0)
      i18n
      polyamorous (~> 1.1)
    rdoc (4.2.2)
      json (~> 1.4)
    request_store (1.3.1)
    responders (2.3.0)
      railties (>= 4.2.0, < 5.1)
    rspec-core (3.5.1)
      rspec-support (~> 3.5.0)
    rspec-expectations (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-mocks (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-rails (3.5.1)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.5.0)
      rspec-expectations (~> 3.5.0)
      rspec-mocks (~> 3.5.0)
      rspec-support (~> 3.5.0)
    rspec-support (3.5.0)
    rubyzip (1.2.0)
    sass (3.4.22)
    sass-rails (5.0.5)
      railties (>= 4.0.0, < 6)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    sdoc (0.4.1)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    seedbank (0.4.0)
    selenium-webdriver (3.0.0)
      childprocess (~> 0.5)
      rubyzip (~> 1.0)
      websocket (~> 1.0)
    simple_form (3.2.1)
      actionpack (> 4, < 5.1)
      activemodel (> 4, < 5.1)
    simplecov (0.12.0)
      docile (~> 1.1.0)
      json (>= 1.8, < 3)
      simplecov-html (~> 0.10.0)
    simplecov-html (0.10.0)
    slop (3.6.0)
    spring (1.7.2)
    sprockets (3.6.3)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.1.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    sqlite3 (1.3.11)
    sshkit (1.11.1)
      net-scp (>= 1.1.2)
      net-ssh (>= 2.8.0)
    thor (0.19.1)
    thread_safe (0.3.5)
    tilt (2.0.5)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    uglifier (3.0.0)
      execjs (>= 0.3.0, < 3)
    warden (1.2.6)
      rack (>= 1.0)
    websocket (1.2.3)
    xpath (2.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  active_model_serializers (~> 0.10.2)
  auto_strip_attributes (~> 2.0, >= 2.0.6)
  awesome_print
  better_errors (~> 2.1, >= 2.1.1)
  binding_of_caller (~> 0.7.2)
  bootstrap-kaminari-views
  bootstrap-sass (~> 3.3)
  byebug (~> 9.0)
  capistrano (~> 3.5)
  capistrano-bundler (~> 1.1, >= 1.1.4)
  capistrano-rails (~> 1.1, >= 1.1.7)
  capistrano-rvm (~> 0.1.2)
  capistrano-scm-copy (~> 0.7.0)
  capybara (~> 2.7, >= 2.7.1)
  coffee-rails (~> 4.1.0)
  database_cleaner (~> 1.5, >= 1.5.3)
  devise
  devise_ldap_authenticatable
  fabrication (~> 2.15, >= 2.15.2)
  jbuilder (~> 2.0)
  jquery-rails
  kaminari
  launchy (~> 2.4, >= 2.4.3)
  meta_request
  paper_trail (~> 5.2, >= 5.2.2)
  paranoia (~> 2.0)
  pg
  pry-byebug (~> 3.4)
  puma (~> 3.4)
  quiet_assets
  railroady
  rails (= 4.2.6)
  ransack
  responders (~> 2.3)
  rspec-rails (~> 3.4)
  sass-rails (~> 5.0)
  sdoc (~> 0.4.0)
  seedbank (~> 0.4.0)
  selenium-webdriver (~> 3.0)
  simple_form
  simplecov (~> 0.12.0)
  spring
  sqlite3
  squeel!
  uglifier (>= 1.3.0)

BUNDLED WITH
   1.13.2

michael-reeves avatar Nov 11 '16 01:11 michael-reeves

Does rake routes list that URL? Looks like it should be entities, rather than entity

NullVoxPopuli avatar Nov 11 '16 10:11 NullVoxPopuli

rake routes

api_v1_entities GET    /api/v1/entities(.:format)    api/v1/entities#index {:format=>"json"}
                POST   /api/v1/entities(.:format)    api/v1/entities#create {:format=>"json"}
api_v1_entity GET    /api/v1/entities/:name(.:format)    api/v1/entities#show {:format=>"json"}
              PATCH  /api/v1/entities/:name(.:format)    api/v1/entities#update {:format=>"json"}
              PUT    /api/v1/entities/:name(.:format)    api/v1/entities#update {:format=>"json"}
              DELETE /api/v1/entities/:name(.:format)    api/v1/entities#destroy {:format=>"json"}

ip_address belongs_to :entity

michael-reeves avatar Nov 15 '16 17:11 michael-reeves

I just realized your specifically not using the block form of links. is there a reason why?

NullVoxPopuli avatar Nov 15 '16 18:11 NullVoxPopuli

@michael-reeves are you using JSONAPI adapter? Because, if that's the case, having an attribute named "links" is forbidden (ref. http://jsonapi.org/format/#document-resource-object-attributes).

groyoh avatar Nov 15 '16 20:11 groyoh

@NullVoxPopuli, @groyoh This is the section of the relationship links how-to that I was following:

Links as an attribute of a resource

This is applicable to JSONAPI, JSON and Attributes adapters

You can define an attribute in the resource, named links.

class Api::V1::UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :links

  def links
    {
      self: api_v1_user_path(object.id),
      microposts: api_v1_microposts_path(user_id: object.id)
    }
  end
end

This will resilt in (example is in jsonapi adapter):

{
  "data": {
    "id": "1",
    "type": "users",
    "attributes": {
      "name": "Example User",
      "links": {
        "self": "/api/v1/users/1",
        "microposts": "/api/v1/microposts?user_id=1"
      }
    }
  }
}

From what you're telling me, that doc is incorrect?

michael-reeves avatar Nov 17 '16 18:11 michael-reeves

@michael-reeves yeah https://github.com/rails-api/active_model_serializers/blob/d39dd04c11270ade7acd78bdcc1715c281ffd7fc/docs/howto/add_relationship_links.md#links-as-an-attribute-of-a-resource that's to put a link object in an attribute. It's also unfortunate that it's not tested code. So sorry we merged that https://github.com/rails-api/active_model_serializers/pull/1909

oy https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#links https://github.com/rails-api/active_model_serializers/blob/b29395b0ac160c2ee28cc333467954eaafd917bc/docs/general/rendering.md#links

Wanna help fix?

bf4 avatar Nov 17 '16 19:11 bf4

@michael-reeves it looks like this doc does not respect the JSONAPI spec then. I'll see if I can update it. The proper way to add link would to use link(:entity) { api_v1_entity_url(object.entity.name) } like you did.

groyoh avatar Nov 18 '16 13:11 groyoh

Thanks @groyoh

Still, it's very curious to me that the url helper does not even function within the method. Do you know why that is?

michael-reeves avatar Nov 18 '16 17:11 michael-reeves

@michael-reeves Not really sure. Have a look here. I got a working gist with usage of url helpers from the link. Do you require "active_model_serializers" before or after "require "rails"? If I'm not wrong, rails should be require'd first.

groyoh avatar Nov 18 '16 22:11 groyoh

@groyoh, Rails is near the beginning of my Gemfile, and active_model_serializer is further down

The strange thing about it, is it works correctly for link(:entity) like I had commented out. It just doesn't seem to work within the method links

source 'https://rubygems.org'

gem 'simple_form'
gem 'devise'
gem 'devise_ldap_authenticatable'
gem 'ransack'
gem 'paper_trail', '~> 5.2','>= 5.2.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'

# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem 'bootstrap-sass', '~> 3.3'

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks have been removed to fix problem with table sorting
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
# gem 'turbolinks'

# This can fix potential issues with turbolinks and jquery plugins (table ones in particular)
# gem 'jquery-turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.0'

# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'paranoia', '~> 2.0'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Puma as the app server
gem 'puma', '~> 3.4'

# Use Capistrano for deployment
gem 'capistrano', '~> 3.5', group: :development
gem 'capistrano-rails','~> 1.1', '>= 1.1.7', group: :development
#gem 'capistrano-scm-gitcopy', group: :development
gem 'capistrano-scm-copy', '~> 0.7.0'
gem 'capistrano-bundler', '~> 1.1', '>= 1.1.4'
gem 'capistrano-rvm', '~> 0.1.2'

gem 'squeel', git: 'https://github.com/activerecord-hackery/squeel.git'
gem 'awesome_print'
gem 'kaminari'
gem 'bootstrap-kaminari-views'
gem 'auto_strip_attributes', '~> 2.0', '>= 2.0.6'

# Seed multiple environments
gem 'seedbank', '~> 0.4.0'

# Use serializers to render JSON in the API
gem 'active_model_serializers', '~> 0.10.2'
gem 'responders', '~> 2.3'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'sqlite3'
  gem 'pry-byebug', '~> 3.4'
  gem 'byebug', '~> 9.0'

  gem 'rspec-rails', '~> 3.4'

  # Access an IRB console on exception pages or by using <%= console %> in views
  # gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  #gem 'bullet'

  gem 'quiet_assets'
  #gem 'ruby-growl'

  gem 'railroady'
  gem 'meta_request' # for RailsPanel
end

group :development do
  gem 'better_errors', '~> 2.1', '>= 2.1.1'
  gem 'binding_of_caller', '~> 0.7.2'
end

group :test do
  gem 'capybara', '~> 2.7', '>= 2.7.1'
  gem 'selenium-webdriver', '~> 3.0'
  gem 'launchy', '~> 2.4', '>= 2.4.3'
  gem 'fabrication', '~> 2.15', '>= 2.15.2'
  gem 'database_cleaner', '~> 1.5', '>= 1.5.3'
  gem 'simplecov', '~> 0.12.0', require: false
end

group :lab, :lab_test, :production, :prod_adc, :prod_idc do
  gem 'pg'
end

michael-reeves avatar Nov 21 '16 02:11 michael-reeves

I think you are referring to these docs which are wrong (the spec does not allow an attribute named links).

beauby avatar Nov 21 '16 03:11 beauby

@beauby yes. I understand that. However, I would still expect a URL helper to function correctly in a method, no matter what that method is called. The error message I was getting said the URL helper method did not exist.

NoMethodError - undefined method `api_v1_entity_url' for #<IpAddressSerializer:0x007fe3028e40a8>:
  app/serializers/ip_address_serializer.rb:8

if it works for link, it should work anywhere in the serializer.....at least that would be my expectation. Perhaps I'm wrong in that expectation.

michael-reeves avatar Nov 21 '16 03:11 michael-reeves

@michael-reeves I wouldn't be sure – the link block is instance_eval'd in a specific context where some url helpers appear to be mixed in.

beauby avatar Nov 21 '16 03:11 beauby

ok. maybe that's it. I don't want to beat a dead horse here. If you all feel that this issue is resolved with the documentation corrections, feel free to close the issue.

But I'm actually watching the Rails Patterns tutorial on Code School right now. They have url helpers inside of methods within the serializer. So it does seem to function, or at least the helpers did at one time.

michael-reeves avatar Nov 21 '16 03:11 michael-reeves

@michael-reeves looks like the code school videos are out of date and don't actually tell you what version they're on. I think it's 0.8 but not sure.

bf4 avatar Nov 21 '16 05:11 bf4

@beauby

I just tried this exactly like it's described in Code School's Rails Patterns course. The method entity calls a url helper, and I still get the error undefined method api_v1_entity_url' for #IpAddressSerializer:0x007fa6dec8b360`

So at some point in the past, this should have worked exactly like this, but now it doesn't. This has nothing to do with the links method. It appears to just be broken.

class IpAddressSerializer < ActiveModel::Serializer
  # type :ip_address

  attributes :id,
             :address,
             :gateway,
             :netmask,
             :entity,
             :ip_use,
             :ip_type,
             :created_at,
             :updated_at

  def entity
    api_v1_entity_url(object.entity.name)  # also tried with object.entity as the arg
  end

  def ip_use
    object.ip_use.try(:name)
  end

  def ip_type
    object.ip_type.try(:name)
  end
end

michael-reeves avatar Nov 21 '16 17:11 michael-reeves

@michael-reeves

So at some point in the past, this should have worked exactly like this, but now it doesn't. This has nothing to do with the links method. It appears to just be broken.

As I wrote above:

@michael-reeves looks like the code school videos are out of date and don't actually tell you what version they're on. I think it's 0.8 but not sure.

The videos refer to a different version of AMS than you are using. That's why the videos aren't 1:1 with your code.

In any case, this issue has been helpful in finding a bug in the docs https://github.com/rails-api/active_model_serializers/issues/1971#issuecomment-261347824 which is being fixed in https://github.com/rails-api/active_model_serializers/issues/1981

bf4 avatar Nov 21 '16 17:11 bf4

@bf4 Thanks. If that's the answer, then I accept it.

Still it's very strange to me that this is the only place I've seen in the Rails stack where a URL helper doesn't function.

michael-reeves avatar Nov 21 '16 17:11 michael-reeves

@michael-reeves Note that you could simply mix the helper in ActiveModel::Serializer:

class ActiveModel::Serializer
  include Rails.application.routes.url_helpers
end

beauby avatar Nov 21 '16 17:11 beauby

Thank you @beauby.

That gets me the behavior I was looking for.

michael-reeves avatar Nov 21 '16 17:11 michael-reeves

Still it's very strange to me that this is the only place I've seen in the Rails stack where a URL helper doesn't function.

@michael-reeves they also don't work in records :)

bf4 avatar Nov 22 '16 01:11 bf4

Also ref https://github.com/rails-api/active_model_serializers/issues

bf4 avatar Jan 02 '17 15:01 bf4

Also ref https://github.com/rails-api/active_model_serializers/issues/578

bf4 avatar Jan 02 '17 15:01 bf4

Same issue here with modules.

#<NoMethodError: undefined method `room_messages_path' for #<ActiveModelSerializers::Adapter::JsonApi::Link:0x007f89a1cece08>>
module Artemis
  module Talk
    class RoomSerializer < ApplicationSerializer # :nodoc:
      attributes :id, :name

      has_many :messages do
        link(:related) { room_messages_path room_id: object.id }
        include_data false
      end
    end
  end
end
Artemis::Talk::Engine.routes.draw do
  resources :rooms, only: :index do
    resources :messages, only: :index
  end
end
ActiveModelSerializers.config.adapter = :json_api

Accessing the rails console inside the host app, I can access using the follow command:

irb(main):004:0> app.artemis_talk.room_messages_path room_id: 1
=> "/talk/rooms/1/messages"

brunowego avatar Mar 25 '17 23:03 brunowego

I'm guessing it's not completely related, but thought I'd document it in case any others stumble across this.

In my serializer: link(:self) { account_url(object) }

url_helpers were failing for me too, but my log complained about: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I updated each environment with the appropriate host: Rails.application.routes.default_url_options[:host] = 'domain.com'

This worked for me. Just thought I'd share.

benbabics avatar May 22 '17 12:05 benbabics

I somehow had the same problem and could fix it by doing as follow

  link(:chart) do
    scope.controller.fake_route_to_chart_url({
      id: object.id,
      format: :svg
    })
  end

I am not a huge fan of including url_helpers since we already have access to the view_context / controller.

In my case I didn't use view_context, because for some reasons, it doesn't exist when rendering is generating so I had to pass through scope.

nekogami avatar Nov 21 '17 02:11 nekogami