Forum: Ruby on Rails Multi language pages (access @session from a model)

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.
F14efd8e36012e3878f6c02138e57160?d=identicon&s=25 Matthias Pronk (Guest)
on 2005-12-24 19:47
Hi,

I'm creating a simple Ruby on Rails based CMS. It will support multiple
languages. Therefore I've a table 'pages' and a table
'page_translations'. The table 'page' contains a title and body in the
default language, the table 'page_translations' contain a title and body
for an other language.

Now I would like to have an easy way to retrieve the title and body of a
page given the current language (stored in the session). I would like to
put this logic in a model (the Page model, or maybe a new model like
'TranslatedPage'). This way I don't have to put ugly code in my
controllers and views everytime to fetch the page for the current
language.

However, it seems I can't access @session from the model. Does anybody
have a good  idea on how to implement this?

Thanks in advance!

Greetings,
- Matthias
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 Craig Webster (Guest)
on 2005-12-24 20:30
(Received via mailing list)
On 24 Dec 2005, at 18:47, Matthias Pronk wrote:
> However, it seems I can't access @session from the model. Does anybody
> have a good  idea on how to implement this?

Try dropping the @ -- use session instead of @session. Any good?

Yours,
Craig
--
Craig Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
Eb96620826312516bb4a2d6deeee6688?d=identicon&s=25 Jean-Christophe Michel (Guest)
on 2005-12-24 22:28
(Received via mailing list)
Matthias Pronk a écrit :
> controllers and views everytime to fetch the page for the current
> language.
>
> However, it seems I can't access @session from the model. Does anybody
> have a good  idea on how to implement this?

My 0.02$: I think you should normalize your tables, and have

DEFAULT_LANG= 'en'

page_translations
  id
  lang         CHAR(2) DEFAULT en
  mycontent    TEXT

then in your controller you simply do

MyController.show
  @my_model = MyModel.find(12, :include => :page_translations,
                      :conditions => ['lang = ?', session[:lang]])
end

Why is this ugly ?
This topic is locked and can not be replied to.