Linking to and from view pages that include different models, controllers, and objects

I've created a marketplace site where my users (sellers) create collections and add listings to each collection. My shoppers can now shop by listings (Home page) or by collection (Collections page). This Collections page displays the first 3 listing images in each collection along with the name of the collection. Now, I want to be able to click the collection name and go to the actual individual collection page showing all listings in that collection. I created a user shop page that displays all collections from that user. From that shop page I can click on the collection name and go to the individual collections page. But I want to also click from the main site Collections page directly to the individual collection page, bypassing having to go through the shop page first. I need to define my User method and create a link_to. Right now my main Collections controller defines; @collections = Collection.includes(:listings).order(created_at: :desc). My individual collections page controller has: @user = User.find(params[:id]) -- @listings = Listing.where(collection: params[:collection_id]) -- @collection = Collection.find(params[:collection_id]). The link_to on the Collections view page is: <%= link_to "#{}", shopcollected_path(collection) %>. The error I get when I click on the link is: "ActiveRecord::RecordNotFound in ListingsController#shopcollected" and "Couldn't find User with 'id'=13". The 13 is the collection id. So I'm needing to define my User somewhere so it links the listings through the user to the collection id? Is that right?

Posted about 5 years ago by Amy Peterson
Posted almost 5 years ago by Alex Yang

If I'm understanding correctly, it seems that from your controller code, you're expecting two parameters: id and collection_id. But from your link_to, you're only passing one parameter: collection.

What's your URL format for the 'shopcollected' page? You may wish to include the id and collection_id within the URL itself, so it looks like{USER_ID}/{COLLECTION_ID}. Remember that any parameters you include in the URL you must pass in the link_to route. You'll be able to set the URL format in your config/routes.rb page.

Let me know how it goes.

Posted almost 5 years ago by Amy Peterson

Still struggling...I changed my routes for the individual 'shopcollected_path' to be:

get '/listings/sections' => 'listings#sections', as: 'sections'
get '/shopcollections/:id' => 'listings#shopcollections', as: 'shopcollections'
get '/shopcollected/:id/:collection_id' => 'listings#shopcollected', as: 'shopcollected'

And it works just fine when I'm on the users' shop page viewing all collections ('shopcollections.html.erb') by that user; I can click on any collection name and view the individual 'shopcollected.html.erb', and the URL looks like: http://localhost:3000/shopcollected/9/13
( 9=user_id, 13=collection_id )

<%= link_to "#{}", shopcollected_path(collection_id: collection) %>

But when I'm on my global site page showing all collections from all users ('sections.html.erb'):
<%= link_to "#{}", shopcollected_path( ?? WHAT GOES HERE ?? ) %>

Why wouldn't this work the same since the route to the 'shopcollected_path' is now specifically calling the user id and then the collection id?

Posted almost 5 years ago by Alex Yang

Hi Amy,

First off, my apologies for not being able to reply to your email sooner. I'm traveling in China at the moment and Gmail is often blocked (though BaseRails works!). I just saw your email though and I'm glad that you were able to figure things out. It's tough to learn the hard way, but the upshot is that you'll remember it for a long time :-)

I just wanted to clarify when to use the colon versus the comma. Using something like 'shopcollected_path(collection_id: collection)' means that you're passing a single variable named 'collection_id' that has the value of 'collection'. On the other hand, using 'shopcollected_path(collection_id, collection)' means that you're passing two variables: 'collection_id' and 'collection'. Hope that makes sense and please send me a link to your site. I'd love to check it out!