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

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.
F0fb7fb81c0604bf4c6747df54930b3f?d=identicon&s=25 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.
700f9a3b883e6a04d018f48290b1a3fd?d=identicon&s=25 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
This topic is locked and can not be replied to.