在Rails中向控制器添加新视图

Tre*_*zel 8 routes ruby-on-rails ruby-on-rails-3

我有一个控制器,clients_controller具有相应的索引,显示,编辑,删除,新的和表单视图.有没有办法创建一个新的视图,就像clients/prospects.html.erb行为一样clients/index.html.erb,除了路由clients/prospects/

我试过这个:

match '/clients/prospects' => 'clients#prospects'
Run Code Online (Sandbox Code Playgroud)

还有一些其他的东西routes.rb,但当然得到错误"找不到具有id =潜在客户的客户".

这里的目标基本上是拥有潜在客户视图和客户视图,并且通过简单地将隐藏字段切换为1,它(在用户的脑海中)将潜在客户转变为客户端(它是类似CRM的应用程序).

mig*_*igu 7

你需要做几件事.首先,您需要任何通用路线之前放置自定义路线.否则Rails假定"前景"这个词是show动作的id.例:

get '/clients/prospects' => 'clients#prospects' # or match for older Rails versions
resources :clients
Run Code Online (Sandbox Code Playgroud)

您还需要在ClientsController中复制/粘贴索引方法并将其命名为prospect.例:

class ClientsController < ApplicationController
  def index
    @clients = Client.where(prospect: false)
  end

  def prospects
    @prospects = Client.where(prospect: true)
  end
end
Run Code Online (Sandbox Code Playgroud)

最后,您需要复制index.html.erb视图并将副本命名为prospects.html.erb.在上面的示例中,您将不得不使用@prospects实例变量.

  • 如果客户端/潜在客户**与客户端/索引完全相同,则不必复制视图.相反,您可以在控制器中加载潜在客户后添加`render:template =>'clients/index'`.如果要使用相同的视图,还必须将变量命名为@clients而不是@prospects.这也适用于格雷格特的回答. (2认同)