# Dumb views?

**URL:** <https://www.ruby-forum.com/t/dumb-views/100882>\
**Category:** Rails\
**Created:** [August 1, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/dumb-views/100882 "2006-08-01T19:54:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/1 "2006-08-01T19:54:28Z")

</div>

Hello again! I’ve another “what would you do in this situation”  
question.

You’ve got 6 controllers, one named base. The other 5 controllers extend  
base. Currently they share one layout.

Your layout displays navigation. However, there are two actions inside  
base  
for which you do not want navigation displayed (index, new).

Do you…

a) Add logic to your layout telling it to conditionally display the  
navigation.  
b) Create an entirely different layout with no navigation and have the  
actions in question arbitrarily render that layout.

Let’s hear it! 🙂

- Rabbit

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/2 "2006-08-01T20:23:22Z")

</div>

On Aug 1, 2006, at 1:53 PM, Rabbit wrote:

> Do you…
> 
> a) Add logic to your layout telling it to conditionally display the  
> navigation.  
> b) Create an entirely different layout with no navigation and have  
> the actions in question arbitrarily render that layout.
> 
> Let’s hear it! 🙂

What about:  
c) have navigation() be a helper function called from the layout that  
can be over-ridden in the appropriate helper class or controller  
method (just remember to make it protected)

-Mat

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/3 "2006-08-01T20:29:33Z")

</div>

Hmm… I had thought about making a helper method for navigation, but  
thought it was too much like option A - logic in the view.

However, I kinda dig your idea of overwriting it for specific parts of  
the  
application. And I suppose if it were in a helper then I could define  
which  
“tab” is the active one. I’ll try that.

Thanks Mat. 🙂

- Rabbit

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/4 "2006-08-01T20:55:42Z")

</div>

Here’s what I’ve got for a first-run:  
[http://rafb.net/paste/results/myCYgs62.html](http://rafb.net/paste/results/myCYgs62.html)

For some reason, it feels very PHP-ish. Any thoughts?

- Rabbit

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/5 "2006-08-01T22:38:42Z")

</div>

What’s bottom posting? I’m accessing the list via Gmail… I just  
checked  
out what the thread looks like on Ruby Forum and it doesn’t look  
confusing  
to me. =/ (Or are you suggesting I quote what I’m referring to?)

Anyway, yeah that’s a lot, but it’s also wicked. Sadly I’ve never  
thought  
about calling render() from a helper. 🙂

I’ll spend some time on your suggestions and let you know how it turns  
out.

Thanks again Mat, you rock! 🙂

- Daniel

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/6 "2006-08-01T22:22:40Z")

</div>

On Aug 1, 2006, at 2:53 PM, Rabbit wrote:

> Here’s what I’ve got for a first-run:  
> [http://rafb.net/paste/results/myCYgs62.html](http://rafb.net/paste/results/myCYgs62.html)
> 
> For some reason, it feels very PHP-ish. Any thoughts?
> 
> - Rabbit

First thought: bottom post 🙂

The first things we can try to get rid of is that “tabs” variable.  
Sadly, the pattern isn’t exactly consistent so we have to get  
tricky. But we do have a little bit of patterning going on in  
everything except calendar.

You can factor that out (at it’s simplest idea) by adding a method  
like so  
[‘Calendar’, ‘Schedules’, ‘Blackouts’, ‘Ticket Templates’, ‘Venue  
Layout’].each do |tab|  
if current\_page?(location\_for\_name(tab))  
…link to…  
else  
…link to…  
end  
end

location\_for\_name would turn ‘Ticket Templates’ into  
‘ticket\_templates’ by string manipulation (there may already be a  
method somewhere for this) and have a special case for Calendar.

Finally, I dislike html in methods, so you might want to consider  
embedding this logic into a layout partial with some imbedded code.  
The final function might be something like:

def navigation  
@tabs = ‘Calendar’, ‘Schedules’, ‘Blackouts’, ‘Ticket Templates’,  
‘Venue Layout’]  
render :template =\> ‘layouts/navigation’  
end

def location\_for\_name tab\_name  
if tab\_name == ‘Calendar’  
{ :controller =\> ‘tickets’, :action =\> ‘calendar’ }  
else  
{:controller =\> tab\_name.gsub(’ ', ‘\_’).downcase, :action =\> ‘index’ }  
end  
end

def link\_for\_name tab\_name  
if current\_page?(location\_for\_name(tab))  
link\_to tab\_name, location\_for\_name(tab\_name), {:class =\> “active”}  
else  
link\_to tab\_name, location\_for\_name(tab\_name), {:class =\> “active”}  
end  
end

with navigation.rhtml being

\<% for tab in @tabs do %\> - \<%= link\_for\_name tab %\>
\<% end %\>

The moral of the story is: Ruby makes introducing new functions easy  
and quick. So do it.

To make this even cooler and more ruby-ish, we could make all the  
controllers subclasses of some parent that had an inherited()  
function defined and kept track of it’s children. Then instead of  
loose functions, we can call .as\_link or .title or .location on the  
controller classes themselves. Doing this would yield a  
naviation.rhtml like:  
\<% for controller in MainController.children do %\>

- \<%= controller.to\_link %\>
  
\<% end %\>

MainController then contains the munging functions and the Tickets  
controller (the oddball) overrides .name and .location with it’s own  
idea of what a tab should be.

Wow… that was a lot. Sorry if I’m confusing you.

-Mat

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 2, 2006, 12:43am UTC](https://www.ruby-forum.com/t/dumb-views/100882/7 "2006-08-02T00:43:11Z")

</div>

On Aug 1, 2006, at 4:36 PM, Rabbit wrote:

> Thanks again Mat, you rock! 🙂

I like to help. And it was an issue I tackled not long ago. As for  
bottom posting…

See how I’m placing messages under the quoted text? That’s bottom  
posting. You’re top posting. What you’re reading on google is also  
a mailing list, so bottom posting helps list subscribers (like me)  
follow the conversation more easily.

An example that cracked me up:

A: Indeed it does!  
On Aug 1, 2006, at 12:00 PM, A Dude wrote:

> Q: Does top posting suck?

I’m sure you can see, it’s much easier to read the question first,  
then the answer, rather than the reverse.  
-Mat

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 2, 2006, 1:20am UTC](https://www.ruby-forum.com/t/dumb-views/100882/8 "2006-08-02T01:20:21Z")

</div>

> On Aug 1, 2006, at 4:36 PM, Rabbit wrote:  
> See how I’m placing messages under the quoted text?

Yes. 🙂

> You’re top posting.

Not anymore!

- Daniel

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [August 1, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/dumb-views/100882/9 "2006-08-01T23:54:37Z")

</div>

Alright. Let’s see…

First up, in my base controller I’m already using a before\_filter. So  
I’ve defined my tabs there… (I hope all this code comes out okay)

## productions.rhtml (layout)

…  
\<%= navigation %\>  
…

## base\_controller.rb (controller)

def load\_production  
@tabs = [ ‘Calendar’, ‘Schedules’, ‘Blackouts’, ‘Ticket Templates’,  
‘Venue Layout’ ]  
session[:production\_id] = params[:id] if params[:id]  
@production = Production.find(session[:production\_id])  
rescue  
flash[:negative] = %{Oops, couldn’t find a valid production.}  
redirect\_to(:controller =\> ‘base’)  
end

## base\_helper.rb (helper)

def link\_to\_tab(tab)  
if current\_page?(path\_for\_tab(tab))  
# Active tab.  
link\_to(tab, path\_for\_tab(tab), { :class =\> ‘active’ })  
else  
link\_to(tab, path\_for\_tab(tab))  
end  
end

def path\_for\_tab(tab)  
case tab  
when ‘Calendar’  
{ :controller =\> ‘tickets’, :action =\> ‘calendar’ }  
else  
{ :controller =\> tab.to\_id, :action =\> ‘index’ }  
end  
end

def navigation  
html = ‘

’

@tabs.each do |tab|  
html \<\< %{

- } \<\< link\_to\_tab(tab) \<\< %{
}  
end

html \<\< ‘

’  
end

(I injected a bit of html in my helper, shoot me! 🙂

The #to\_id method is an extension of string I wrote. (With inspiration  
from this very mailing list.)

class String

def to\_id  
downcase.gsub(/\W/, ’ ‘).squeeze.strip.gsub(’ ', ‘\_’)  
end

end

As a added bonus, using an array instead of a hash lets me arbitrarily  
order the nav items!

_Once again!_ thanks Mat. 🙂

- Daniel
