-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
url_helpers non-functional in links method? #1971
Comments
Does rake routes list that URL? Looks like it should be entities, rather than entity |
rake routes
ip_address |
I just realized your specifically not using the block form of links. is there a reason why? |
@michael-reeves are you using JSONAPI adapter? Because, if that's the case, having an attribute named |
@NullVoxPopuli, @groyoh Links as an attribute of a resourceThis 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 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 #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? |
@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 |
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 Not really sure. Have a look here. I got a working gist with usage of url helpers from the |
@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 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 |
I think you are referring to these docs which are wrong (the spec does not allow an attribute named links). |
@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.
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 I wouldn't be sure – the |
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 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. |
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 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 |
As I wrote above:
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 #1971 (comment) which is being fixed in #1981 |
@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 Note that you could simply mix the helper in class ActiveModel::Serializer
include Rails.application.routes.url_helpers
end |
Thank you @beauby. That gets me the behavior I was looking for. |
@michael-reeves they also don't work in records :) |
Also ref #578 |
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" |
I'm guessing it's not completely related, but thought I'd document it in case any others stumble across this. In my serializer: url_helpers were failing for me too, but my log complained about: I updated each environment with the appropriate host: This worked for me. Just thought I'd share. |
I somehow had the same problem and could fix it by doing as follow
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. |
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:
If I remove the links attr and uncomment link(:entity) piece, I get correct response.
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
Additonal helpful information
(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)
config/initializers/active_model_serializer.rb
Gemfile.lock
The text was updated successfully, but these errors were encountered: