# Some success at last, and some questions

**URL:** <https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166>\
**Category:** Rails\
**Created:** [February 9, 2010, 10:21am UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166 "2010-02-09T10:21:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 9, 2010, 10:21am UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166/1 "2010-02-09T10:21:51Z")

</div>

Hi Everyone,

I’ve been using Ruby roughly since 2003, but I never could understand  
Rails nor do something meaningful with them.  
At last thanks to wonderful people from Rails Guides at  
[Getting Started with Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/getting_started.html) I began to  
understand Rails.

At the moment I am trying to do Rails version of a website I made  
recently using PHP.

I am trying to make series of tabs linking to other pages on the site.  
An I have put following code in views/partials/\_partail1 which is used  
by view of each page, eg. views/page/page.html.erb

\<%  
def checkclass(path)  
uri=request.env[‘REQUEST\_URI’]  
if path==uri  
cl=‘current’  
#cl - class attribute in HTML tag  
else  
cl=‘other’  
end  
end  
tabs=[[‘Home page’,‘/’],  
[‘How we work’,‘/howwework’],  
[‘Contact Us’, ‘/contactus’]]  
%\>

 

\<%  
x=0  
tabs.each do |tab| %\>  
\<%= link\_to tab[0],tab[1], :id=\>x+=1, :class =\> checkclass(tab[1]) %\>

 

\<%  
end  
%\>

 

So here are my questions:  
1 Is it appropriate to put such code here?  
2 how can I display the link in the line above   
without using too many tag like this \<%= %\>. render :text and  
render :inline don’t seem to work, I either get error messages or  
nothing at all.

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 9, 2010, 10:26am UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166/2 "2010-02-09T10:26:32Z")

</div>

On Feb 9, 9:20 am, Bigos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So here are my questions:  
> 1 Is it appropriate to put such code here?

No. You don’t really want any code in your view, other than the very  
bare minimum. Sounds like you want to put a l lot of this in a view  
helper

> 2 how can I display the link in the line above   
> without using too many tag like this \<%= %\>. render :text and  
> render :inline don’t seem to work, I either get error messages or  
> nothing at all.

Why are you concerned about using \<%= ? It’s the right tool for the  
job

Fred

(PS: look at each\_with\_index)

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 9, 2010, 10:32am UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166/3 "2010-02-09T10:32:58Z")

</div>

On Feb 9, 9:24 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Feb 9, 9:20 am, Bigos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So here are my questions:  
> > 1 Is it appropriate to put such code here?
> 
> No. You don’t really want any code in your view, other than the very  
> bare minimum. Sounds like you want to put a l lot of this in a view  
> helper  
> Thanks for prompt reply, I don’t want to put any more code than that.  
> Just want a simple change of CSS style depending which page is being  
> displayed at the moment.

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 10, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166/4 "2010-02-10T16:35:46Z")

</div>

> end

Is it OK to get rig of @current\_tab from controllers and modify above  
tab\_link method to look like following code?

def tab\_link(tab\_name, target)  
link\_to(tab\_name, target, :class =\> target ==  
request.env[‘PATH\_INFO’] ? ‘active\_tab’: ‘other\_tab’)  
end

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 9, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/some-success-at-last-and-some-questions/183166/5 "2010-02-09T15:22:43Z")

</div>

On Tue, Feb 9, 2010 at 4:31 AM, Bigos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > No. You don’t really want any code in your view, other than the very  
> > bare minimum. Sounds like you want to put a l lot of this in a view  
> > helper  
> > Thanks for prompt reply, I don’t want to put any more code than that.  
> > Just want a simple change of CSS style depending which page is being  
> > displayed at the moment.

Well you already have WAY too much code in the view, and that code  
deals with stuff like urls/routing which really shouldn’t be the  
business of a view.

The way I approach the issue of highlighting the active tab (which it  
looks like is what this code is trying to do, would be to have a  
helper to generate the html, which uses either the controller name (if  
there’s a one-one correspondence between controllers and tabs) or a  
current\_tab instance variable set by the controller. And the helper  
should use path/url helpers rather than coding absolute uris.

So something like this.

First make sure that routes.rb has a sensible routing for the various  
urls. Just an example:

map.home ‘/’, :controller =\> ‘welcome’, :action =\> ‘show’  
map.how\_we\_work ‘/how\_we\_work’, :controller =\> ‘welcome’, :action =\>  
‘our\_process’  
map.contact\_us ‘contact\_us’, :controller =\> ‘contacts’, :action =\>  
‘index’

Note that these route are just for example purposes, I’d probably do  
it quite differently, but that’s not relevant to the current problem.  
In fact, the whole point is that the routing is something which might  
well change over the life of the app, and that’s one of the things  
which makes view code like your starting point fragile.

Then in the controller(s) in the action methods which handle the  
various end points

class WelcomeController \< ApplicationController  
def show  
@current\_tab = ‘Home page’  
end

def our\_process  
@current\_tab = ‘How we work’  
end

end

class ContactsController \< ApplicationController  
def index  
@current\_tab = ‘Contact Us’  
end  
end

in a helper, probably application\_helper.rb

def tab\_link(tab\_name, target)  
link\_to(tab\_name, target, :class =\> tab\_name == @current\_tab ?  
‘current\_tab’ : ‘jtab’)  
end

def navigation\_bar  
[  
[‘Home page’, home\_path],  
[‘How we work’,how\_we\_work\_path],  
[‘Contact Us’, contact\_us\_path]  
].map {|tab\_name, target | tab\_link(tab\_name, target)}.join(“\n”)

In the view:

\<%= navigation\_bar %\>

Again this is probably not exactly the code I’d come up with, but it’s  
close to what you started with and adheres much better to what the  
responsibilities of routes, controllers, and views should have in a  
Rails app. IMHO

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)
