好的,我在這裏有點困惑 - 並且可能有一個簡單的解決方案來解決這個問題 - 但過去兩個小時我還沒有弄清楚,所以我想我可能會問。更改link_to中的語言環境,具體取決於db中的屬性i18n
我在DB中使用兩種不同語言編寫博客文章(AT & EN) - 在I18n中定義的語言環境位於(德語)& en(中文)。
每次創建博客條目時,都會將條目與條目一起保存。簡單的例子:
id | title | content | locale
-----------------------------------------
1 | First Blog | Hey, .. | en
2 | Erster Blog | Hallo, . | at
等
所以,一般我把這兩個語言分開的網站上 - 因爲如果這些是兩個不同的網站,但訪問博客簡介的最新博客是一個列表所示。一些博客用德文和英文發帖。
如果訪問者現在在www.mysite.com/en - 並點擊德國博客上的參考文獻,則會發生錯誤。
在控制器我定義如下,以確保相同的內容無法通過兩個URL的訪問:
@blog = Blog.friendly.find(params[:id], :conditions => ["language_id = ?", I18n.locale])
因此,這是爲了確保搜索引擎優化明智的,我不得到懲罰。 (www.mysite.com/en/first-blog == www.mysite.com/at/first-blog)。目前,我趕在模型中的例外:
around_filter :catch_not_found
....
def catch_not_found
yield
rescue ActiveRecord::RecordNotFound
redirect_to blogs_path, :flash => { :error => "Sorry, the Blog isn't available in your language." }
end
的問題是 - 如何傳遞的語言環境中的link_to,使用戶被重定向在正確的區域博客?
感謝
你在哪裏使用的link_to解決問題呢? – LuiGi
偏離主題,但'at'代表德語的縮寫? –
嗨Rich, - at代表奧地利。 LuiGI,我在博客視圖中使用link_to以下方式:show.html.erb:<%@ relatedblogs.each do | blog | %> \t \t \t \t \t \t \t
<%=的link_to blog.title,blog_path(blog.slug):標題=> 「讀取」 + blog.title%>,<(%)= blog.created_at%>
\t \t \t \t \t \t <% end %> –