Forum: Typo Where to get relative links within a view template?

Posted by Tobias Weisserth (polarapfel)
on 2009-03-23 11:24
(Received via mailing list)
Hi everybody,

in my view templates, I have a meta navigation bar in the header, that
renders links to the administration login page, an about page and an 
imprint
page. So far, I hard-coded these links. This is bad.

So what's the proper way to generate these links? The link to the admin 
page
should be easy as it is static. I thought about using the Blog class, I
noticed a method there to retrieve the base url and just concatenate the
admin page behind that.

Would I need to come up with some kind of plugin that does the rest, 
thus
generating links to pages? Or can I touch the routes/controller 
behaviour to
create a mapping there manually after creating the pages I need (about,
imprint)?

thanks,

Tobias W.
Posted by de Villamil Frédéric (Guest)
on 2009-03-23 12:10
(Received via mailing list)
Le 23 mars 09 à 11:16, Tobias Weisserth a écrit :

>
> Would I need to come up with some kind of plugin that does the rest,  
> thus generating links to pages? Or can I touch the routes/controller  
> behaviour to create a mapping there manually after creating the  
> pages I need (about, imprint)?
>
> thanks,
>
> Tobias W.


Hi Tobias,

We have some very helpful helpers to help (obvious heh) us make nice,
not hardcoded links in our views. The most important is base_url,
which is included into this_blog.

So to get to your admin, I would use:

<%= link_to "Admin", "#{this_blog.base_url}/admin" %>

You also have link_to_permalink which is kinda helpful. For example,
with pages, you can do:

<% pages = Page.find(:all, :conditions => {:state => 'published'}) %>
<% pages.each do |page| %>
<%= link_to_permalink(page, page.title) %>
<%end%>

And so on...

Cheers,
Frédéric

--
Frédéric de Villamil
"What's mine is mine. What's yours is still unsetteled" – Go player
proverb
frederic@de-villamil.com                        tel: +33 (0)6 62 19 1337
http://t37.net                       Typo : http://typosphere.org
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.