Nick,

Thanks again for the reply.

I think I will probably go back to using either protected controller
methods or helper methods to fetch the reference data. I agree with
you 100% in that this type of logic simply has no place in the view.

As for my statement regarding the framework lacking a way to easily,
and DRYly, fetch reference data... I am just surprised that there is
not any kind of shortcut/helper for loading reference data now.
Perhaps a function built in to ActiveController such as
"load_ref_data" which could optionally take an argument that would
specify the name of the action to load data for (would default to
current action), and could potentially use some type of naming
convention to call a method that gets all of the reference data for
the action. I'm sure there are more clever ways to do this, and this
is just off the top of my head.

So you might have something in you controller such as...

def new
  load_ref_data
  ...
end

def create
  ...
  if @myObject.save
    ...
  else
    load_ref_data :new
    render :action => :new
end

protected

def ref_data_new
  @categories = Category.find(...)
  @states = State.find(...)
  ...
end

You might even add an option to the render method itself, such as
"render :action => :new, :include_ref_data => true".

I will probably end up rolling my own system, but I am surprised with
all the things that they thought of for Rails, that they didn't think
of something like this.

- Justin

On Nov 16, 11:13 am, Nick Hoffman <[EMAIL PROTECTED]>
wrote:
> G'day Justin.
>
> > That's a great suggestion. I would actually prefer using a helper
> > method over putting the call to ModelObject#find in the view itself.
> > Something about calling find directly from the view just doesn't feel
> > right to me. The only reason I'm not 100% opposed to it is because a
> > call to find is usually going to be a 1-liner.
>
> It might be a one-liner right now, but once you add error handling, it's
> longer. And what's error handling?...It's logic, which doesn't belong in
> views.
>
> > I am new to Rails, but based on my perception of MVC, fetching data
> > from the model is a task that is meant to be done by the controller,
> > and the view should simply be handed that data by the controller.
>
> Yup! Absolutely.
>
> > However, looking through the Rails documentation itself, I saw the
> > same method mentioned by Elias (calling find directly in the view) in
> > examples within the ActionView::FormOptionsHelper class, so I guess
> > that is a Rails convention? Or was it just done for example purposes?
>
> There could be all sorts of reasons why the example's writer put the
> call to #find in the view. Regardless, "The Rails Way" is to not put
> logic in views.
>
> > So far, I have been absolutely blown away by almost all the features
> > that Rails has to offer, but it almost seems like the designers of the
> > framework overlooked, or didn't feel it was important, to add a
> > simple, and DRY, way to fetch reference data from the controller.
>
> I think you might be confusing the act of "fetching" reference data with
> the act of formatting and/or displaying it. Controllers share data (IE:
> variables) with views via instance variables, or passing variables to
> the ":locals" option of #render. That's pretty simple. If multiple
> controller actions need to share the same data, simply write a protected
> or private controller method, or add a method to the model in question,
> that all of these controller actions can call.
>
> > I am quite surprised by this, as it is a very common task to fetch
> > reference data for the purpose of populating controls such as select/
> > drop-down boxes. I was also surprised at the lack of message board and
> > blog posts about this subject. Perhaps, as I mentioned, the method
> > that Elias made reference to is the Rails convention, and I am just
> > missing something :)
>
> As you know, views shouldn't contain any sort of logic. If you're
> thinking about calling #find within a view, what will you do with the
> data that's returned? Most of the time, you'll need to check if any
> records were returned. After that, you'll iterate through the Array, and
> display some data.
>
> That's "business logic", which doesn't belong in views. But it does
> belong in helpers!
> -Nick
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to