1

In my Rails application I have an Images class that is used by Store & Maker, I've set Images up as belong to a polymorphic association through imageable (following most of the standard docs and Railscasts), most of it works, however when I add a back button to an image I'm not sure what I'm actually meant to put in for the options:

polymorphic_path(@imageable.images)

Doesn't work: image_image_image_image_image_path is the resulting thrown error, and various different combinations of ([@imageable, images]) and so on haven't helped. @imageable definitely points to the correct class though, if I leave it as polymorphic_path(@imageable) it'll return to the show for the passed object.

I'm guessing I've misunderstood how I'm passing the objects in but I can't see where.

Edit: just to clarify, by back I mean returning to the index of images for the object.

Edit: just to expand on what I've tried, polymorphic_url returns the same issue. imageable_images_path doesn't work as it's not a :resource in routes.rb, [@imageable, images] returns that images is an undefined.

Edit: To save turning this into a giant I'll add the relevant files as gists:

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55

1 Answers1

1

What about polymorphic_url

Something like polymorphic_url(@imageable.images) or even just polymorphic_url(images) might work

EDIT: I think maybe the problem is the @imageable

according to the documentation:

# calls post_url(post)
polymorphic_url(post) # => "http://example.com/posts/1"
polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
polymorphic_url(Comment) # => "http://example.com/comments" 

So when you pass polymorphic_path(@imageable) and it gives you the relevant show page, that makes sense because passing @imageable is in fact passing a specific instance of imageable - /stores/X/images/1 or /stores/X/images/2, etc. If you want to go to the index, you don't want a specific instance of imageable - you want only images

This is untested, but try either

polymorphic_path(store, :images) / polymorphic_url(store, :images)

or

polymorphic_path(@imageable, :action => 'index')

source: https://stackoverflow.com/a/6205831/2128691

Community
  • 1
  • 1
dax
  • 10,779
  • 8
  • 51
  • 86
  • I've tried both `polymorphic_url` and `polymorphic_path`. I'll update the question with that information. – Nicholas Smith Aug 15 '13 at 09:24
  • Kudos to you good sir, you weren't 100% there but you were pretty damn close. It's actually `polymorphic_path([@imageable, :images])`, so a combination of a few things. – Nicholas Smith Aug 15 '13 at 20:12