在我的网站上,我有一个全局页面,其中显示了所有卖家创建的所有收藏集(sections.html.erb)。每个集合中都有多个产品。该集合在集合表中具有一个id以及一个user_id列。
我的collection.rb文件具有以下内容:
has_many :listings, dependent: :destroy
belongs_to :user
我的user.rb文件具有以下内容:
has_many :collections, dependent: :destroy
has_many :listings, dependent: :destroy
我想直接从全局主页面链接到集合的详细信息页面,该页面显示了该集合中的所有产品(shopcollected.html.erb)。我可以从卖家商店集合页面(shopcollections.html.erb)成功链接到该详细信息页面,但是我无法从全局主页上获取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,所以主要的全局链接看起来像:
www.website.com/shopcollected/{USER_ID}/{COLLECTION_ID}
但是我已经尽力想了一切,但仍然无法正常工作。有人知道我需要在“ sections.html.erb” link_to中传递什么,是否需要再次更改路线,如果需要,则更改为什么?
注意:如果我将“ shopcollected”路线更改为其他路线,则无法再从“ shopcollections”页面链接至该路线。我需要保持原样,但要从“ sections.html.erb”页面添加链接。
路线:
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) %>
我的座右铭:永不放弃:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句