Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Chuck Han's Profile

GitHub User: prototypos

Site: http://www.prototypos.com

Comments by Chuck Han

Avatar

@Phillip (I know, more than a year later):
I think your issue is the same as mine. For example, in Ryan's example, if Photos were nested in Articles, find_commentable would not work correctly. Assuming a RESTful API, then you need to retrieve $1 from the request.request_uri (note my example uses 'imageable' as opposed to 'commentable'):

    def find_imageable
      pathArray = request.request_uri.split('/')
      if pathArray.count > 2
        if pathArray.count % 2 == 0
          imageableString = pathArray[pathArray.count-3].singularize
        else
          imageableString = pathArray[pathArray.count-4].singularize
        end
        @which_id = imageableString+"_id"
        if pathArray.count % 2 == 0
          return imageableString.classify.constantize.find(pathArray[pathArray.count-2])
        else
          return imageableString.classify.constantize.find(pathArray[pathArray.count-3])
        end
      else
        params.each do |name, value|
          if name =~ /(.+)_id$/
            @which_id = name
            return $1.classify.constantize.find(value)
          end
        end
      end
      nil
    end