【问题标题】:Trying to link from one view page to another through different models试图通过不同的模型从一个视图页面链接到另一个视图页面
【发布时间】: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


【解决方案1】:

这是一个路由问题:

在 Rails 4 中,您可以访问:http://localhost:3000/rails/info/routes 并查看您的所有路线。

您的shopcollected_path(collection) 正确地转到您的shopcollected_path,但:id 是集合ID。但是在您的操作中,您正在通过 :id 寻找用户并且它中断了。

如果您希望集合由用户限定,那么您将必须在路由中的某处传递用户 ID。我认为您想要的是嵌套路由:

编辑

您似乎总是想为 shopcollectionsshopcollected 操作查找用户。

路线

get '/pages/sections' => 'pages#sections', as: 'sections'
resources :users do
  get 'shopcollections/:id' => 'listings#shopcollections', as: 'shopcollection'
  get 'shopcollected/:id' => 'listings#shopcollected', as: 'shopcollected'
end

listings_controller.rb

def shopcollections
  @user = User.find(params[:user_id])
  @shopcollection = @user.collections.find(params[:id])
  @listings = @shopcollection.listings # I imagine this is a relationship?
end   

def shopcollected
  @user = User.find(params[:user_id])
  @shopcollection = @user.collections.find(params[:id])
  @listings = @shopcollection.listings # I imagine this is a relationship?
end

我对你们的关系做了一些假设,但我认为这正是你们想要的。

【讨论】:

  • 但是我不需要 2 条路线,一条用于主要部分页面 (sections.html.erb),另一条用于用户商店收藏页面 (shopcollections.html.erb)?如果我编辑 shopcollections 路线,这不会影响我当前工作的用户个人资料收藏页面“shopcollections”吗?
  • 对不起,我明白你的意思了。我编辑了我的资源:用户以添加“做资源:shopcollections”。然后我改变了我的link_to。然后我需要更改控制器中的任何内容吗?
  • 您必须更改控制器操作,以便它们通过 :id Collection.find(params[:id]) 查找集合并按集合 ID Listing.where(collection_id: params[:id]) 列出,然后通过 :user_id User.find(params[:user_id]) 查找用户。
  • 抱歉有点丢了。我为 :users 编辑了我的路线。然后我要删除我的 get /shopcollections/:id 吗?然后当我编辑我的 Controller#sections 时,我的页面现在因错误而中断:“找不到具有 'id'=”的用户。
  • 谢谢埃文,我会试一试,然后告诉你。
猜你喜欢
  • 2015-12-12
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
相关资源
最近更新 更多