rails视图中有多少代码可以吗?

时间:2022-10-22 11:24:50

I know that it is best to keep code out of the presentation layer. But, I am wondering how much is considered "acceptable". For example I populate an html select box with this line of code.

我知道最好将代码保留在表示层之外。但是,我想知道多少被认为是“可接受的”。例如,我用这行代码填充一个html选择框。

CodesecureProject.find(:all,:order => 'name').collect {|project| [project.name, project.id] }

Right now I have this line of code embedded in the form. What I am wondering if the community thinks if this is to much code and it should be first stored in an instance variable on the controller then the variable used in the form.

现在我在表单中嵌入了这行代码。我想知道社区是否认为这是多少代码,它应该首先存储在控制器上的实例变量中,然后是表单中使用的变量。

4 个解决方案

#1


5  

I use the following static method in a Site model to achieve something similar:

我在Site模型中使用以下静态方法来实现类似的操作:

class Site
  def self.select_options
    Site.find(:all, :order => 'UPPER(name)').collect {|s| [s.name, s.id]}
  end
def

Then in my Domain view I call:

然后在我的域视图中,我调用:

<%= f.select :site_id, Site.select_options %>

This works really well for these circumstances.

这对于这些情况非常有效。

In your instance, you might try:

在您的实例中,您可以尝试:

class CodesecureProject
  def self.select_options
    CodesecureProject.find(:all, :order => 'name').collect {|p| [p.name, p.id]}
  end
end

And then call it through the view with:

然后通过视图调用它:

<%= f.select :codesecure_project_id, CodesecureProject.select_options %>

#2


5  

I'm not going to say I'd never do it (I'd be lying) but the code example given would make me nervous. I think I'd be more inclined to deliver the data to the select box from my controller. A helper method is another option if I notice I'm doing something more than once. I'm more likely to see the duplication in the controller than across distinct views.

我不会说我永远不会这样做(我会说谎)但是给出的代码示例会让我感到紧张。我想我更倾向于将数据传送到我的控制器的选择框中。如果我注意到我不止一次做某事,那么辅助方法是另一种选择。我更有可能在控制器中看到重复,而不是在不同的视图中。

If I'm using the same HTML component across multiple views, then I might find myself reaching for partials or wrapping the whole thing in a custom helper: project_select() or some such.

如果我在多个视图中使用相同的HTML组件,那么我可能会发现自己会触及部分或将整个事物包装在自定义帮助器中:project_select()或其他类似的东西。

The more I work in the MVC world the more I find myself avoiding code in views. I have a feeling that some kind of Zen mastery will be achieved if I reach the zero code state, although the value of that in anything but philosophical terms is highly debatable.

我在MVC世界中工作的越多,我就越发现自己在视图中避免使用代码。我有一种感觉,如果我达到零代码状态,将会实现某种禅的掌握,尽管除了哲学术语之外的任何东西的价值都是值得商榷的。

#3


1  

I have a lot of the same code in my projects except I try to don't do any finds. In your case I would make an named scope

我的项目中有很多相同的代码,除了我尝试不做任何发现。在你的情况下,我会做一个命名范围

named_scope :order, lambda { |order| {:order => order}}

and make the code:

并制作代码:

CodesecureProject.order(:name).collect {|project| [project.name, project.id] }

It's a little cleaner.

它有点清洁。

If you got a lot of select boxes which need a name and an id (I sure do sometimes), you could also try making a helper that excepts a ModelName and returns the array you need.

如果你有很多需要名字和id的选择框(我确实有时会这样做),你也可以尝试制作一个除了ModelName的助手并返回你需要的数组。

def magic_for_select(model)
  model.all.collect{|instance| [instance.name, instance.id]}
end

#4


1  

I would go a bit further than Maran. Generally I do the following:

我会比Maran更进一步。通常我会做以下事情:

  • Create a named_scope in the model to execute the find.
  • 在模型中创建named_scope以执行查找。
  • Call the named_scope from the controller and store the results in an instance variable.
  • 从控制器调用named_scope并将结果存储在实例变量中。
  • Only put the instance variable in the view.
  • 仅将实例变量放在视图中。

I would only use a helper if absolutely necessary. When going back over your code later, it's easier to make sense of things if you see your controller setting up the data that the view needs, rather than the view calling the helper (yet another file to look at).

如果绝对必要,我只会使用帮助器。稍后回顾代码时,如果您看到控制器设置了视图所需的数据,而不是调用帮助程序的视图(还有另一个要查看的文件),则更容易理解。

#1


5  

I use the following static method in a Site model to achieve something similar:

我在Site模型中使用以下静态方法来实现类似的操作:

class Site
  def self.select_options
    Site.find(:all, :order => 'UPPER(name)').collect {|s| [s.name, s.id]}
  end
def

Then in my Domain view I call:

然后在我的域视图中,我调用:

<%= f.select :site_id, Site.select_options %>

This works really well for these circumstances.

这对于这些情况非常有效。

In your instance, you might try:

在您的实例中,您可以尝试:

class CodesecureProject
  def self.select_options
    CodesecureProject.find(:all, :order => 'name').collect {|p| [p.name, p.id]}
  end
end

And then call it through the view with:

然后通过视图调用它:

<%= f.select :codesecure_project_id, CodesecureProject.select_options %>

#2


5  

I'm not going to say I'd never do it (I'd be lying) but the code example given would make me nervous. I think I'd be more inclined to deliver the data to the select box from my controller. A helper method is another option if I notice I'm doing something more than once. I'm more likely to see the duplication in the controller than across distinct views.

我不会说我永远不会这样做(我会说谎)但是给出的代码示例会让我感到紧张。我想我更倾向于将数据传送到我的控制器的选择框中。如果我注意到我不止一次做某事,那么辅助方法是另一种选择。我更有可能在控制器中看到重复,而不是在不同的视图中。

If I'm using the same HTML component across multiple views, then I might find myself reaching for partials or wrapping the whole thing in a custom helper: project_select() or some such.

如果我在多个视图中使用相同的HTML组件,那么我可能会发现自己会触及部分或将整个事物包装在自定义帮助器中:project_select()或其他类似的东西。

The more I work in the MVC world the more I find myself avoiding code in views. I have a feeling that some kind of Zen mastery will be achieved if I reach the zero code state, although the value of that in anything but philosophical terms is highly debatable.

我在MVC世界中工作的越多,我就越发现自己在视图中避免使用代码。我有一种感觉,如果我达到零代码状态,将会实现某种禅的掌握,尽管除了哲学术语之外的任何东西的价值都是值得商榷的。

#3


1  

I have a lot of the same code in my projects except I try to don't do any finds. In your case I would make an named scope

我的项目中有很多相同的代码,除了我尝试不做任何发现。在你的情况下,我会做一个命名范围

named_scope :order, lambda { |order| {:order => order}}

and make the code:

并制作代码:

CodesecureProject.order(:name).collect {|project| [project.name, project.id] }

It's a little cleaner.

它有点清洁。

If you got a lot of select boxes which need a name and an id (I sure do sometimes), you could also try making a helper that excepts a ModelName and returns the array you need.

如果你有很多需要名字和id的选择框(我确实有时会这样做),你也可以尝试制作一个除了ModelName的助手并返回你需要的数组。

def magic_for_select(model)
  model.all.collect{|instance| [instance.name, instance.id]}
end

#4


1  

I would go a bit further than Maran. Generally I do the following:

我会比Maran更进一步。通常我会做以下事情:

  • Create a named_scope in the model to execute the find.
  • 在模型中创建named_scope以执行查找。
  • Call the named_scope from the controller and store the results in an instance variable.
  • 从控制器调用named_scope并将结果存储在实例变量中。
  • Only put the instance variable in the view.
  • 仅将实例变量放在视图中。

I would only use a helper if absolutely necessary. When going back over your code later, it's easier to make sense of things if you see your controller setting up the data that the view needs, rather than the view calling the helper (yet another file to look at).

如果绝对必要,我只会使用帮助器。稍后回顾代码时,如果您看到控制器设置了视图所需的数据,而不是调用帮助程序的视图(还有另一个要查看的文件),则更容易理解。