Rails:两个实例变量或仅一个并在视图中创建另一个实例变量

发布于 2024-12-27 16:55:05 字数 312 浏览 0 评论 0原文

我对 Rails 的礼貌有疑问,但我总体上相信 MVC。我有一个控制器,它在给定特定条件的情况下从模型中检索一些对象。我需要视图中的这些对象,并且还需要具有这些对象属性的特定数据结构。该数据结构是这样的:

[[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...]

我的问题是:

我应该在控制器中创建两个实例变量:一个包含从模型检索的所有对象,另一个包含从这些对象的属性创建的数据结构,或者我应该创建一个包含所有对象的实例变量并在视图中创建该数据结构?

I have a doubt of good manners in Rails and I believe MVC in general. I have a controller which retrieves some objects from a model given a particular condition. I need those objects in the view, and I also need a particular data structure with attributes of those objects. That data structure is like this:

[[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...]

My question is:

Should I create two instance variables in the controller: one with all the objects retrieved from the model, and another one with the data structure created from the attributes of those objects or should I just create one instance variable with all the objects and create that data structure in the view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2025-01-03 16:55:05

这取决于情况,但您不应该在视图中创建数据结构。

可以在控制器函数中执行此操作,或者如果它与模型紧密耦合,则在模型中执行此操作。从有限的描述来看,这听起来像是属于模型,但很难说。

It depends, but you shouldn't create data structures in the view.

Either do it in a controller function, or if it's tightly coupled to the model, in the model. From the limited description it sounds like this belongs in the model, but it's hard to say.

孤单情人 2025-01-03 16:55:05

您可以将一些代码从视图中移出:

  1. 视图助手
  2. 控制器
  3. 模型

Rails Helper > (选项 1)似乎更适合您的情况。例如,如果您需要填充选择标记,则使用返回 options_for_select(...your complex structure formatted) 的方法的帮助器将是正确的选择。

您可以这样使用 options_for_select (来自 rails 指南

<%= options_for_select([[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...], 2) %> 将变为:

<option value="object1-attr2-value">object1-attr1-value</option>
<option value="object2-attr2-value" selected="selected">object2.attr1-value</option>
...

您可以在 ApplicationHelper 内(如果需要在应用程序范围内)或模型特定的帮助程序中添加该方法:

# app/helpers/...the helper you choose
def options_for_your_complex_select(default_state)
  options_for_select( [[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...],
                  default_state )
end

并在您的视图中使用此帮助程序方式(请选择比我的更好的方法名称:)): <%= select_tag :state, options_for_your_complex_select(params[:default_state]) %>

这会有所帮助您可以使您的代码更易于管理,并且您的视图更具可读性。
Rails 最佳实践网站对此有更多信息。了解如何在 ViewHelper 中移动代码 a>、模型控制器

如果您必须处理大量属性,@ryanb 的有关 draper gem 的截屏视频会进行解释如何简化你的观点。

You could move some of your code out of your views to:

  1. a view helper
  2. the controller
  3. the model

A Rails Helper (opt. 1) seem more appropriate for your case. If you need for example to populate a select tag an helper with a method that returns options_for_select(...your complex structure formatted) would be the right choice.

You can use options_for_select (from the rails guides) this way

<%= options_for_select([[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...], 2) %> will become:

<option value="object1-attr2-value">object1-attr1-value</option>
<option value="object2-attr2-value" selected="selected">object2.attr1-value</option>
...

You can add that method inside the ApplicationHelper (if it needs to be application wide) or in a model specific helper:

# app/helpers/...the helper you choose
def options_for_your_complex_select(default_state)
  options_for_select( [[object1.attr1, object1.attr2],[object2.attr2,object2.attr2],...],
                  default_state )
end

and the use this helper in your views this way (please choose a better method name than mine :) ): <%= select_tag :state, options_for_your_complex_select(params[:default_state]) %>

This will help you make your code more manageable and your view more readable.
Rails best practices website has more on this. See how move your code in a ViewHelper, Model and Controller.

If you have to deal with a lot of attributes, this screencast about the draper gem by @ryanb explain how to simplify your views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文