How much code in rails mode is okay?

I know that it is best to leave the code from the view level. But, I wonder how much is considered "acceptable." For example, I populate the html selection box with this line of code.

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

Now I have this line of code embedded in the form. What interests me is whether the community is thinking whether this is a lot of code, and it must first be stored in an instance variable on the controller, then the variable used in the form.

+5
source share
4 answers

I use the following static method in a site model to achieve something like this:

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

Then in my domain I call:

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

This works well for these circumstances.

:

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

:

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

, ( ), . , . - , , - . , .

HTML , : project_select() .

MVC, . , - , , , , .

+5

, , . scope

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

:

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

.

, ( ), , ModelName, .

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

, . :

  • named_scope .
  • named_scope .
  • .

I would use only an assistant, if absolutely necessary. When you return through your code later, it’s easier to understand things if you see that your controller sets up the data that is needed for the view, and not the view that calls the helper (another file to view).

+1
source

All Articles