在 link_to 中传递参数

Passing parameters in link_to

在我的站点中,我有一个全局页面,其中显示了所有卖家 (sections.html.erb) 创建的所有 collection。每个 collection 里面有多个产品。 collection 在 collection 中有一个 id 和一个 user_id 列 table.

我的 collection.rb 文件有这个:

has_many :listings, dependent: :destroy
belongs_to :user

我的 user.rb 文件有这个:

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

我想 link 从全局主页面直接转到 collection 的详细信息页面,该页面显示 collection (shopcollected.html.erb) 中的所有产品.我可以从卖家商店 collection 的页面 (shopcollections.html.erb) 成功 link 到这个详情页面,但我无法从主全局 link_to 工作页。

我今天将我的路线更改为'/shopcollected/:id/:collection_id',所以我想认为我在'shopcollections.html.erb'页面上传递的link_to参数可以与主全局页面相同 link_to。但显然这不是因为我在 '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]

我显然缺少参数 :id 所以主全局 link 看起来像:

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

但是我已经尝试了所有我能想到的方法,但我仍然无法让它工作。有谁知道我需要在我的 'sections.html.erb' link_to 中传递什么,如果我需要再次改变我的路线,如果是的话,到什么?

注意:如果我将 'shopcollected' 路线更改为其他路线,那么我将无法再从 'shopcollections' 页面 link 到它。我需要保持完整,但从 'sections.html.erb' 页面添加 linking。

路线:

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

控制器

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 ?? ) %>

非常感谢任何帮助。

我终于让它工作了。换成我的'sections.html.erb':

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

我的座右铭:永不放弃 :)