Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中调用操作时调用其他操作_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在Rails中调用操作时调用其他操作

Ruby on rails 在Rails中调用操作时调用其他操作,ruby-on-rails,ruby,Ruby On Rails,Ruby,假设我有userscoontroller#show操作。它默认呈现users/show.html.erb 现在,我还有ProductsController#index,它接收一个Clientid,并显示一个包含属于该客户机的所有产品的表。它呈现products/index.html.erb 当用户想要查看UsersController\show时,我想显示此产品列表。以下是一些这样做的想法: 将userscoontroller#show重定向到productscoontroller#index

假设我有
userscoontroller#show
操作。它默认呈现
users/show.html.erb

现在,我还有
ProductsController#index
,它接收一个
Client
id,并显示一个包含属于该客户机的所有产品的表。它呈现
products/index.html.erb

当用户想要查看
UsersController\show
时,我想显示此产品列表。以下是一些这样做的想法:

  • userscoontroller#show
    重定向到
    productscoontroller#index
  • UsersController#show
    中呈现
    products/index.html.erb
  • products/index.html.erb
    复制到
    users/show.html.erb

然而,我觉得这些解决方案并不是我想要的最好的解决方案。还有其他更好的选择吗?如果没有,我应该遵循上面哪项?

为什么不在显示方法下的UsersController中传递与该用户相关的产品?你可以这样做

def show
  @products = Products.where(user_id: @user.id).paginate(:page => params[:page], :per_page => 3)
end
或者,如果你使用的是Desive,我会这么做

def show
  @products = Products.where(user_id: current_user.id).paginate(:page => params[:page], :per_page => 3)
end
然后在前面,您可以通过调用


为了进行分页,我使用了will_paginate gem

创建一个带有产品列表的部分,并在需要时进行渲染。产品列表必须基于一些过滤器从其操作中获取产品,并且还应支持分页;如果使用partial.Than,则不确定所有这些都可以完成,而不是让一个类负责项目搜索和分页,并在UsersController中使用它,如果我还需要单独查看产品,该怎么办?在客户的展示页面上展示产品只是进入客户的展示页面,然后单击链接查看其产品的快捷方式。这是某种默认行为。您也可以在products controller中使用ProductSearch类。它将类似于附加的表示层。