RailsCasts Pro episodes are now free!

Learn more or hide this

FlintMakal's Profile

GitHub User: FlintMakal

Comments by

Avatar

To adapt the magic sauce with nested resources :

ruby
def find_commentable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
  nil
end

i did :

ruby
def find_commentable
    params.each_with_index do |elem, index|
        if index == params.length - 1
            if elem[0].to_s =~ /(.+)_id$/
               return $1.classify.constantize.find(elem[1])
             end
        end
    end
  end

which allow to find the last controller param with its Id , and for me works well to get the @commentable type and id

Hope it helps someone, cheers