Forum: Ruby on Rails Right way to pass conditionals to partials

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jowan P. (Guest)
on 2006-04-15 07:20
Hi All,

I have some partials that need to be rendered a bit differently
depending on the context.  To make matters worse, sometimes these
partials are including other partials, that I'd like to control from the
main template.  These partials are  templates that look similar in
different contexts, but have different controls depending on where
they're being rendered.  I tried locals, but the result looks ugly - I
basically have to do this:

<% if !defined?(nocontrols) && !nocontrols ->
html to render controls
<% end ->

Then in my main template I call <% render(:partial => "partial", :locals
=> { :nocontrols => true }) >

This works, but the convoluted if statements make me cringe.  Plus, I'd
have to specifically pass these locals to any nested partials, and that
gets too complicated.

Declaring locals in controllers work, but then I can't include two
partials with different parameters....

There has to be a better way to do this.
Brez! !. (Guest)
on 2006-04-16 00:04
> There has to be a better way to do this.

What abt a Helper() ?  If nothing else you could consolidate the
convoluted IF statements.. Looks like youre tyring to do a role based
exposure of controls , e.g. an admin can see this button, etc.  If so it
might be worth considering some kind of container that would have the
html controls [do don't this in the model tho - bad violation of MVC]
for a given roll - then the view [your .rhtml] can simply render whats
in the container [probably just a plain old ruby object] - the
controller would of course populate the container based on the role of
the current user. This way your view simply renders whats there [which
could mean nothing, if applicable].. this would alleviate the need for
any role-type processing in your partials, i.e. avoid the problem all
together.
This topic is locked and can not be replied to.