【发布时间】:2015-03-06 00:10:46
【问题描述】:
在我的站点中,我有一个全局页面,其中显示了所有卖家创建的所有集合 (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参数对于主要的全局可以是相同的页面链接到。但显然这不是因为我在“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 ?? ) %>
非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 parameters link-to