On Feb 10, 12:09 am, Adam Stegman <adam.steg...@gmail.com> wrote:
> On Feb 8, 9:16 am, Marnen Laibow-Koser <li...@ruby-forum.com> wrote:
> > Andrew France wrote:
> > > On 09/02/10 01:32, Marnen Laibow-Koser wrote:
> > >> Andrew France wrote:
>
> > >>> Hi,
>
> > >>> I was thinking whether it would be more elegant for a view to call
> > >>> methods on the controller rather than rely on instance variables.
>
> > >> The trouble with this is that controller methods are actions, not
> > >> getters.
>
> > > It's quite common for controllers to have methods which are not directly
> > > actions.
>
> > Not in my applications.  It's inappropriate to put that much logic in
> > the controller.
>
> > The only exceptions are things like current_user, which can't really
> > live anywhere else, and current_object, which is simply a refactoring of
> > controller code.  Anything else belongs in a model or a helper.
>
> I completely agree with Marnen. Fat models, skinny controllers.
>
> Your example is a poor one - it's certainly appropriate to have a
> @book instance variable in the BooksController. Your instinct is
> correct about lots of instance variables, but they should usually be
> replaced with model instance methods.

Which is a philosophy I try to subscribe to, but there are many
instances where I wish to pass multiple values to a view. Perhaps
because the data is not related in the database or I wish to control
the dataset that is passed to the view.

For example, if I have a show action for Book, as a convenience I may
wish to display all the authors on the system that the user can
access:
def show
  @book = Book.find(params[:id])
  @all_authors = Author.all
end

Is that not reasonable?
It's a contrived and simplified example of what I deal with in most
apps, where I need to get data from unrelated models. Or sometimes I
may create instance variables from related data (e.g. @authors =
@book.authors) because I wish to re-use the view in other controllers.

So with my suggestion it could be written as (short block syntax):
var(:book) { Book.find(params[:id]) }
var(:all_authors) { Author.all }
var(:authors) { book.authors }

Is that better or worse? Harder to read or improves comprehension? Or
has no purpose at all?

The problem with instance variables is that my view is not inside the
controller class, so it's reasonable to ask if it's appropriate for
these variables to appear 'outside' of their class. It is also easy to
forget to set the variable in the controller, which is often done on
purpose and checked for in view logic. But by replacing instance
variables with methods you are creating a more explicit interface
contract that you expect one or more controllers to adhere to.

Thanks,
Andrew

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to