首页 \ 问答 \ Rails 4 - 最佳实践 - 如何使用来自不同控制器的数据显示视图?(Rails 4 - Best practices - How to display a view with data from different controllers?)

Rails 4 - 最佳实践 - 如何使用来自不同控制器的数据显示视图?(Rails 4 - Best practices - How to display a view with data from different controllers?)

我正在构建一个Rails 4应用程序。

该应用程序用于为一堆客户端(IT咨询业务将使用的应用程序)记录网络硬件设备。 第一个索引视图是列出所有客户端的视图。 此时,您可以单击链接以查看特定于该客户端的项目。 我已经为我正在记录的不同类型的东西(即服务器控制器,防火墙控制器等)构建了不同的模型和控制器。

我已经到了嵌套的地步,我可以从客户端索引页面获取链接以传递client.id并在该SPECIFIC控制器上查看与该客户端关联的项目(即它将列出该客户端的所有服务器) 。 现在,当你在那里时,我希望它也列出来自不同型号/控制器的所有其他项目。 我想要一个显示来自不同型号/控制器的数据的页面。

我正在尝试确定这样的场景的最佳实践。 我觉得我错过了显而易见的事情,并且Rails以某种特定的方式处理这个问题 - 或者 - 大多数人说Rails糟透了/不这样做,每个人都做X来完成这个任务。

以下是我没有特别顺序的想法:

  1. 使用partials - 似乎不合适,因为我理解的是为了简化你的视图代码
  2. 渲染 - 这里关注的是如何传递ID以获取正确的客户端
  3. 在控制器中操作之前嵌套/使用以设置client.id,然后使用所有其他控制器的实例变量编写索引操作。 已经为嵌套执行此操作并且它可以工作,但仅适用于包含client.id的原始请求链接。 我无法弄清楚如何将client.id传递给其他实例变量,以便显示正确的数据。 我觉得我可以解决这个问题,但似乎我走错了路。
  4. JavaScrip / AJAX类型的解决方案真的让动态内容发生?

我想确保我在干爽的DRY意义上做到这一点。


I'm building a Rails 4 application.

The application serves to document network hardware devices for a bunch of clients (an app an IT consulting business would use). First index view is one that lists all the clients. At that point you can click on a link to view items specific to that client. I've built out different models and controllers for the different types of things I'm documenting (i.e. servers controller, firewalls controller, etc.).

I've gotten to the point with nesting where I can get the link from the client index page to pass along the client.id and view items associated with that client on that SPECIFIC controller (i.e. it will list all the servers for that client). Now when you are there I want it to also list all the other items that are from different models/controllers as well. I want one page that displays data from the different models/controllers.

I'm trying to determine what best practice is for a scenario like this. I feel like I'm missing the obvious and that Rails deals with this in some specific way - or - that most people say Rails sucks/does not do this and everyone does X to accomplish this task.

Here are the ideas I have in no particular order:

  1. Use partials - seems out of place as partials as I understand are to simplify your view code
  2. Render - concern here is how do you pass the ID to get the correct client
  3. Nesting/Use before action in the controller to set the client.id and then write index actions with instance variables for all the other controllers. Already doing this for the nesting and it works, but just for the original request link that contained the client.id. I can't figure out how to pass the client.id to the other instance variables so the correct data is displayed. I feel like I could fix this bug but it seems like I'm going down the wrong path.
  4. JavaScrip/AJAX type of solution to really make dynamic content happen??

I want to make sure I'm doing this in the good ole fashioned DRY sense.


原文:https://stackoverflow.com/questions/26941962
更新时间:2023-08-28 18:08

最满意答案

您当前的select语句应该引发错误,因为在使用group by时,select子句中只存在group by子句或aggrigation函数中存在的列。

正确的查询非常类似于您发布的查询,只是没有组和正确的显式连接(隐式连接已经过时了20多年),并将这些连接转换为左连接:

SELECT p.pId, p.pName, s.sId, s.sName, g.gId, g.gName
FROM table1 as p,
LEFT JOIN table2 as s ON p.pId=s.pId
LEFT JOIN table3 as g ON s.sId=g.sId

这应该会得到以下输出:

pId  pName  sId  sName  gId  gName
1    p1     11   s1     111  g1
1    p1     11   s1     112  g2
2    p2     12   s1     null null

Your current select statement should raise an error, since when using group by, only columns that exists in the group by clause or in an aggrigation function can exist in the select clause as well.

The correct query is very much like the one you posted, just without the group by and with proper, explicit joins (implicit joins are outdated for over 20 years now), and turning these joins to left joins:

SELECT p.pId, p.pName, s.sId, s.sName, g.gId, g.gName
FROM table1 as p,
LEFT JOIN table2 as s ON p.pId=s.pId
LEFT JOIN table3 as g ON s.sId=g.sId

This should get you the following output:

pId  pName  sId  sName  gId  gName
1    p1     11   s1     111  g1
1    p1     11   s1     112  g2
2    p2     12   s1     null null

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)