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

时间:2021-12-01 23:08:36

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:

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

[[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?

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

2 个解决方案

#1


1  

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

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

  1. a view helper
  2. 一个助手

  3. the controller
  4. 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.

Rails助手(选项1)似乎更适合您的情况。如果您需要例如填充选择标记,则使用返回options_for_select(...复杂结构格式化)的方法的帮助程序将是正确的选择。

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

您可以通过这种方式使用options_for_select(来自rails指南)

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

<%= 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>
...

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

您可以在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

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]) %>

并以这种方式在您的视图中使用此帮助程序(请选择比我更好的方法名称:)):<%= 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.

这将有助于您使代码更易于管理,并使您的视图更具可读性。 Rails最佳实践网站对此有更多的了解。了解如何在ViewHelper,Model和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.

如果您必须处理很多属性,@ryanb关于draper gem的截屏视频将解释如何简化您的视图。

#2


2  

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.

要么在控制器功能中执行,要么在模型中与模型紧密耦合。从有限的描述来看,这听起来像属于模型,但很难说。

#1


1  

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

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

  1. a view helper
  2. 一个助手

  3. the controller
  4. 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.

Rails助手(选项1)似乎更适合您的情况。如果您需要例如填充选择标记,则使用返回options_for_select(...复杂结构格式化)的方法的帮助程序将是正确的选择。

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

您可以通过这种方式使用options_for_select(来自rails指南)

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

<%= 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>
...

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

您可以在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

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]) %>

并以这种方式在您的视图中使用此帮助程序(请选择比我更好的方法名称:)):<%= 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.

这将有助于您使代码更易于管理,并使您的视图更具可读性。 Rails最佳实践网站对此有更多的了解。了解如何在ViewHelper,Model和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.

如果您必须处理很多属性,@ryanb关于draper gem的截屏视频将解释如何简化您的视图。

#2


2  

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.

要么在控制器功能中执行,要么在模型中与模型紧密耦合。从有限的描述来看,这听起来像属于模型,但很难说。