Error when using links:

23 views
Skip to first unread message

Luke Norman

unread,
Nov 1, 2013, 4:49:19 PM11/1/13
to roar...@googlegroups.com
I have a representer for my Location model. A location has many Observations. I would like a link to each observation, like below:

  • {
    • id1,
    • latitude40,
    • longitude50,
    • _links
      {
      • [
        • {
          • href"/observation/1"
          },
        • {
          • href"/observation/2"
          }
        ]
      }
    },

My representer is below:

#This is decorator-style roar. To use, return LocationRepresenter.new(Location)
class LocationRepresenter < Representable::Decorator
  include Roar::Representer::JSON::HAL

  property :id
  property :latitude
  property :longitude

  links :observation do
    represented.observation.map {|o| {rel: nil, href: "/observation/#{o.id}"}}
  end

end #class LocationRepresenter

when I run this, I get the following error:

13:45:42 web.1  | Unexpected error while processing request: undefined method `rel' for nil:NilClass
13:45:42 web.1  | /home/luke/.rvm/gems/ruby-2.0.0-p247/gems/roar-0.12.0/lib/roar/representer/json/hal.rb:139:in `rel'
13:45:42 web.1  | /home/luke/.rvm/gems/ruby-2.0.0-p247/gems/roar-0.12.0/lib/roar/representer/feature/hypermedia.rb:113:in `add'

I've asked on IRC, but I can't understand what I'm doing wrong. Any help greatly appreciated!

Luke Norman

unread,
Nov 1, 2013, 5:06:37 PM11/1/13
to roar...@googlegroups.com
Modifying the following lines in hal.rb fixes this issue for me - not sure if this is a bug, or a problem with the way I'm implementing things:

        class LinkArray < Array
          def rel
            if(first)
              first.rel
            else
              nil
            end
          end

          def rel=(rel)
            each { |lnk| lnk.rel = rel }
          end
        end

Nick Sutterer

unread,
Nov 5, 2013, 11:58:17 PM11/5/13
to roar...@googlegroups.com
Hi Luke, sorry for the late reply. This seems to happen when the array you compile is empty. Someone else already filed a bug report: https://github.com/apotonick/roar/issues/80

Nick


--
You received this message because you are subscribed to the Google Groups "Roar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to roar-talk+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages