【发布时间】:2015-02-25 00:35:20
【问题描述】:
我创建了一个市场网站,我的用户(卖家)在其中创建收藏品,然后将列表添加到每个收藏品以进行销售。任何购物者现在都可以按所有列表(主页)或所有系列(sections.html.erb)购物。部分视图页面上的每个集合都会显示前 3 个列表图像以及集合的名称。
我希望能够从 sections.html.erb 页面直接点击到单个集合页面 shopcollected.html.erb,该页面显示该集合中的所有列表.
另外,我创建了一个用户商店页面,显示该用户的所有收藏品 (shopcollections.html.erb)。从那里我可以成功单击任何集合名称并转到单个集合页面shopcollected.html.erb,一切都很好。
但是当我从 sections.html.erb 中单击集合名称(在本例中为 collection_id=13)时,我收到此错误:
"ActiveRecord::RecordNotFound in ListingsController#shopcollected"
"Couldn't find User with 'id'=13".
网址显示:
http://localhost:3000/shopcollected/13
我看到这是在识别 collection_id,但它没有识别此集合的用户。但我认为我的控制器 def shopcollected 已经定义了用户,那不应该吗?是我的“link_to”中缺少的部分吗?我很困惑...
控制器定义部分:
@collections = Collection.includes(:listings).order(created_at: :desc)
查看文件sections.html.erb链接:
<%= link_to "#{collection.name}", shopcollected_path(collection) %>
控制器定义 shopcollections:
@user = User.find(params[:id])
@listings = Listing.where(collection: params[:collection_id])
@collection = Collection.find(params[:collection_id])
查看文件shopcollections.html.erb链接:
<%= link_to "#{collection.name}", shopcollected_path(collection_id: collection) %>
控制器定义 shopcollected:
@user = User.find(params[:id])
@collection = Collection.find(params[:collection_id])
@listings = Listing.where(collection: params[:collection_id])
路线:
get '/pages/sections' => 'pages#sections', as: 'sections'
get '/shopcollections/:id' => 'listings#shopcollections', as: 'shopcollections'
get '/shopcollected/:id' => 'listings#shopcollected', as: 'shopcollected'
【问题讨论】:
-
您能否从您的路线文件中粘贴 shopcollections 路径?
-
你说collection_id 是13,但是URL 显示的是15。另外,你为什么要在shopcollections 的link_to 中添加collection_id:?我想你会想要像
link_to "text", @collection这样的东西,因为你有资源丰富的路线。 -
@fred,抱歉 15 应该是 13。当我在 link_to 中添加 [at] 集合时,我得到的错误是:“Pages#sections 中的 ActionController::UrlGenerationError”和“没有路由匹配 { :action=>"shopcollected", :controller=>"listings", :id=>nil} 缺少必需的键:[:id]"
标签: ruby-on-rails ruby-on-rails-4 link-to