简体   繁体   中英

Trying to link from one view page to another through different models

I've created a marketplace site where my users (sellers) create collections and then add listings to each collection to sell. Any shopper can now shop by all listings (Home page) or by all collections ( sections.html.erb ). Each collection on the sections view page displays the first 3 listing images along with the name of the collection.

I want to be able to click from the sections.html.erb page directly to the individual collection page shopcollected.html.erb which displays all listings in that collection.

Separately, I created a user shop page that displays all collections from that user ( shopcollections.html.erb ). From there I can successfully click on any collection name and go to the individual collections page shopcollected.html.erb , all good.

But when I click on the collection name (in this case it's collection_id=13) from the sections.html.erb , I get this error:

"ActiveRecord::RecordNotFound in ListingsController#shopcollected"
"Couldn't find User with 'id'=13".

The url shows:

http://localhost:3000/shopcollected/13

I see this is recognizing the collection_id, but it's not recognizing the user for this collection. But I thought my controller def shopcollected already defines the user, so shouldn't that be fine? Is the missing piece in my "link_to"? I'm confused...

Controller def sections :

@collections = Collection.includes(:listings).order(created_at: :desc)

View file sections.html.erb link:

<%= link_to "#{collection.name}", shopcollected_path(collection) %>

Controller def shopcollections :

@user = User.find(params[:id])
@listings = Listing.where(collection: params[:collection_id])
@collection = Collection.find(params[:collection_id])

View file shopcollections.html.erb link:

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

Controller def shopcollected :

@user = User.find(params[:id])
@collection = Collection.find(params[:collection_id])
@listings = Listing.where(collection: params[:collection_id])

ROUTES:

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

This is a routing problem:

In Rails 4, you can visit: http://localhost:3000/rails/info/routes and see all your routes.

Your shopcollected_path(collection) is correctly going to your shopcollected_path , but the :id is the collection id. But in your action, you are looking for the user by :id and it breaks.

If you want the collection to be scoped by user, then you're going to have to pass in the user id somewhere in the route. I think what you want is nested routing:

EDIT

It looks like you always want to look up a User for the shopcollections and shopcollected actions.

routes

get '/pages/sections' => 'pages#sections', as: 'sections'
resources :users do
  get 'shopcollections/:id' => 'listings#shopcollections', as: 'shopcollection'
  get 'shopcollected/:id' => 'listings#shopcollected', as: 'shopcollected'
end

listings_controller.rb

def shopcollections
  @user = User.find(params[:user_id])
  @shopcollection = @user.collections.find(params[:id])
  @listings = @shopcollection.listings # I imagine this is a relationship?
end   

def shopcollected
  @user = User.find(params[:user_id])
  @shopcollection = @user.collections.find(params[:id])
  @listings = @shopcollection.listings # I imagine this is a relationship?
end

I'm making some assumptions about your relationships, but I think that's what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM