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

 手机用户2502886745 发布于 2023-02-12 19:29

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

我试过这个:

match '/clients/prospects' => 'clients#prospects'

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

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

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

    get '/clients/prospects' => 'clients#prospects' # or match for older Rails versions
    resources :clients
    

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

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

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

    2023-02-12 19:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有