Hi,

one pattern I have found useful to remove logic from partials is to
wrap the render call in a helper method. So there is a place to setup
some instance variables or provide default values etc. Of course this
is not a place to put controller logic. But using sufficiently rich
models with scopes and associations my controllers turn out to be
boilerplate most of the time anyway.

Cheers
Tim

On 21 Mrz., 15:44, Nicolás Sanguinetti <[email protected]> wrote:
> On Sun, Mar 21, 2010 at 7:23 AM, [email protected]
>
>
>
> <[email protected]> wrote:
> > Hi,
>
> > I always found one thing severely missing from Rails' partials: an
> > equally easy "Rails" way to not just share view templates easily even
> > across controllers, but also have certain controller code associated
> > with a partial needed to get certain variables for the partial. Right
> > now there is no code associated with a partial, it's just a view
> > template. That's bad sometimes, because not always is the code for
> > setting variables used in the template different between usages of
> > that one template.
>
> > Right now I have to duplicate the code though, and especially when
> > using a template from a different controller this is very awkward,
> > since I cannot just create a new local method for the shared code. I'd
> > have to put it in ApplicationController, for example, but it really
> > belongs into the controller "owning" the particular partial.
>
> > One usage example which I'm using right now:
> > Instead of a different page for the shopping cart, adding addresses
> > and performing payment I want ONE page with all of them, saving the
> > user clicks and letting them see the entire (very short) process at
> > once on a long scroll. This has been proven to lead to a better user
> > experience compared to splitting (a very simple process in my case)
> > into lots of tiny pieces (but even if not, it's what I want).
>
> > As you can see I'd have to duplicate code from shopping_carts/index,
> > addresses/index, orders/new for that page, even though all those
> > pieces of code clearly logically belong into their respective
> > controllers. If I could have partials that automatically run a local
> > "per partial" method of their owning controller when called to
> > render...
>
> > How do YOU guys solve such a situation?
>
> I'd say that if the page uses several resources on a view, they all
> belong to that controller, even if they "have the name" of a different
> one. In any case, if you don't want to pollute ApplicationController
> with methods to load code from other places and you need to reuse the
> logic to load certain resources, write some modules and throw them in
> lib. It's all just ruby, in the end :)
>
> module ShoppingCartData
>   def load_addresses
>     @addresses = current_user.addresses.all
>   end
>   ...
> end
>
> class AddressesController < ApplicationController
>   include ShoppingCartData
>
>   def index
>     load_addresses
>   end
> end
>
> class ShoppingCartsController < ApplicationController
>   include ShoppingCartData
>
>   def show
>     load_addresses
>     ...
>   end
> end
>
> Cheers,
> -foca
>
> > Thanks.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Ruby on Rails: Core" 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 
> > athttp://groups.google.com/group/rubyonrails-core?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" 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-core?hl=en.

Reply via email to