Search code examples
ruby-on-railsruby-on-rails-4parameterslink-to

Passing parameters in link_to


In my site I have a global page which shows all collections created by all sellers (sections.html.erb). Each collection has multiple products in it. The collection has an id and also a user_id column in the collections table.

My collection.rb file has this:

has_many :listings, dependent: :destroy
belongs_to :user

And my user.rb file has this:

has_many :collections, dependent: :destroy
has_many :listings, dependent: :destroy

I want to link from the main global page directly to the collections' detail page which shows all of the products in that collection (shopcollected.html.erb). I can successfully link to this detail page from the sellers' shop collections page (shopcollections.html.erb), but I can't get the link_to to work from the main global page.

I changed my routes today to '/shopcollected/:id/:collection_id', so I want to think that the link_to parameters I am passing on the 'shopcollections.html.erb' page can be the same for the main global page link_to. But clearly it's not because I get the error on 'sections.html.erb':

No route matches {:action=>"shopcollected", :collection_id=>#<Collection id: 21, name: "boden collection 1", created_at: "2015-03-04 21:45:06", updated_at: "2015-03-04 21:45:06", user_id: 13>, :controller=>"listings"} missing required keys: [:id]

I am clearly missing the parameter :id so the main global link would look like:

www.website.com/shopcollected/{USER_ID}/{COLLECTION_ID}

But I've tried everything I can think of and still I can't get it to work. Does anyone know what I need to pass in my 'sections.html.erb' link_to and if I need to change my routes again and if so, to what?

NOTE: If I change my 'shopcollected' route to anything else, then I can no longer link to it from the 'shopcollections' page. I need to keep that in tact, but add linking from the 'sections.html.erb' page.

ROUTES:

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

CONTROLLER

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

def shop
@user = User.find(params[:id])
@listings = Listing.where(user: User.find(params[:id])).order("created_at DESC")
end

def shopcollections
@user = User.find(params[:id])
@collections = @user.collections.order("created_at DESC")
end

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

shopcollections.html.erb:

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

sections.html.erb:

<%= link_to "#{collection.name}", shopcollected_path( ?? WHAT TO PUT HERE ?? ) %>

Any help is greatly appreciated.


Solution

  • I finally got it working. Change to my 'sections.html.erb':

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

    My motto: Never Give Up :)