Ruby on rails 尝试通过不同的模型从一个视图页面链接到另一个视图页面

Ruby on rails 尝试通过不同的模型从一个视图页面链接到另一个视图页面,ruby-on-rails,ruby-on-rails-4,link-to,Ruby On Rails,Ruby On Rails 4,Link To,我已经创建了一个市场网站,我的用户(卖家)在其中创建收藏,然后向每个收藏中添加要出售的物品。任何购物者现在都可以通过所有物品(主页)或所有收藏(sections.html.erb)购物。“截面视图”页面上的每个集合都会显示前3个列表图像以及集合的名称 我希望能够从sections.html.erb页面直接单击到个人收藏页面shopcollected.html.erb,该页面显示该收藏中的所有列表 另外,我创建了一个用户商店页面,显示该用户的所有收藏(shopcollections.html.er

我已经创建了一个市场网站,我的用户(卖家)在其中创建收藏,然后向每个收藏中添加要出售的物品。任何购物者现在都可以通过所有物品(主页)或所有收藏(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".
url显示:

http://localhost:3000/shopcollected/13
我看到这是在识别集合的id,但它没有识别该集合的用户。但是我认为我的控制器defshopcollected已经定义了用户,那不是很好吗?我的“链接到”中缺少的部分是什么?我很困惑

控制器def部分

@collections = Collection.includes(:listings).order(created_at: :desc)
@user = User.find(params[:id])
@listings = Listing.where(collection: params[:collection_id])
@collection = Collection.find(params[:collection_id])
@user = User.find(params[:id])
@collection = Collection.find(params[:collection_id])
@listings = Listing.where(collection: params[:collection_id])
查看文件sections.html.erb链接:

<%= link_to "#{collection.name}", shopcollected_path(collection) %>
<%= link_to "#{collection.name}", shopcollected_path(collection_id: collection) %>
查看文件shopcollections.html.erb链接:

<%= link_to "#{collection.name}", shopcollected_path(collection) %>
<%= link_to "#{collection.name}", shopcollected_path(collection_id: collection) %>
路线:

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

这是一个路由问题:

在Rails 4中,您可以访问:并查看所有路线

您的
shopcollected\u路径(collection)
正确地转到了您的
shopcollected\u路径
,但是
:id
是集合id。但是在您的操作中,您正在按:id查找用户,它会中断

如果希望集合按用户确定范围,则必须在路由中的某个位置传入用户id。我认为您需要的是嵌套路由:

编辑

看起来您总是希望查找用户以执行
shopcollections
shopcollected
操作

路线

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
列表\u 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

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

你能从你的路线文件中粘贴shopcollections路径吗?你说收集id是13,但URL显示15。另外,为什么要在shopcollections内的链接中添加集合\u id:?我想你会想要像
链接到“text”这样的东西,@collection
,因为你有足够的路线。@fred,对不起,15应该是13。当我在link#to中添加[at]集合时,我得到的错误是:“ActionController::UrlGenerationError in Pages#sections”和“No route matches”{:action=>“shopcollected”,:controller=>“listings”,:id=>nil}缺少所需的键:[:id],“但是我不需要两条路由,一条用于主节页面(sections.html.erb)另一个用于用户商店集合页面(shopcollections.html.erb)?如果我编辑shopcollections路线,这不会影响我当前工作的用户配置文件集合页面“shopcollections”?对不起,我明白你的意思。我编辑了我的resources:users以添加“do resources:shopcollections”。然后我把我的链接改为。然后我需要更改控制器中的任何内容吗?您必须更改控制器操作,以便它们通过
:id
集合查找集合。查找(params[:id])
并通过集合id
列表列出。其中(collection\u id:params[:id])
和User by
:User\id
User.find(params[:User\id])。抱歉,有点迷路了。我为:用户编辑了我的路线。然后是否删除我的get/shopcollections/:id?然后当我编辑我的控制器部分时,我的页面现在会出现错误:“找不到id为=”的用户。谢谢Evan,我会尝试一下并让你知道。