awilkening
awilkening

Reputation: 1062

Rails select and collection select helper

This is a preference question I'm sure.

EDIT Recently, I've noticed this code in a few public examples of VIEW code:

f.collection_select :my_method, AddressTypes.all, :name, :name

or

f.select :my_method, ['Option 1', 'Option 2', 'Option 3']

instead of ...

class MyController < ApplicationController

  def new
    @address_types = AddressTypes.all
  end

end

Obviously, in the view:

f.collection_select :my_method, @address_types, :name, :name

Is this just personal preference? Or is there another reason, of which I am not aware?

Upvotes: 1

Views: 2125

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

While I think it comes down to personal preference, even the Rails guides use Model.all right in the view rather than pulling that back to the controller.

My preference is that as soon as I'm doing anything more than Model.all, maybe a Model.where(:foo => "bar") or something, the query should be abstracted back to the controller, or preferably the model. The view shouldn't be responsible for your model/data's logic, it should just be concerned with presenting it.

Upvotes: 4

Related Questions