2012-04-04 47 views
1

我跟隨瑞安Bates railscasts I18n Internationalization並觸及問題/問題。需要診斷routes.rb爲應用程序添加英語/西班牙語Rails 3應用程序

我想設置的語言在我的鏈接,這樣的事情:

http://localhost:3000/en/site/services英語

http://localhost:3000/es/site/services西班牙

我在我的路線文件中定義該這裏:

routes.rb

scope ":locale" do 
    get "site/home" 
    get "site/about_us" 
    get "site/faq" 
    get "site/discounts" 
    get "site/services" 
    get "site/contact_us" 
    get "site/admin" 
    get "site/posts" 
    get "categories/new_subcategory" 
    get "categories/edit_subcategory" 
end 

,我有我的應用程序控制器

before_filter :set_locale 

private 
    def set_locale 
    I18n.locale = params[:locale] if params[:locale].present? 
end 

def default_url_options(options = {}) 
    {locale: I18n.locale} 
end 

而且在我的意見/佈局/ application.html.erb現在

<%= link_to_unless_current "English", locale: "en" %> | 
<%= link_to_unless_current "Spanish", locale: "es" %> 

,每當我試圖運行耙路線或導航到我得到的網址

C:\www\project>rake routes 
    rake aborted! 
    missing :controller 

我是相當新的路線,有人可以幫助我看/解釋問題? 在此先感謝。

回答

1

我剛貼上你發佈的所有代碼到一個新的rails應用程序,它的工作。我的猜測是你的routes.rb文件中有其他路由,其中​​一個是無效的。你貼出來這樣的路線:

[email protected]:~/projects/testproj$ rake routes 
site_home GET /:locale/site/home(.:format)    :locale/site#home 
site_about_us GET /:locale/site/about_us(.:format)  :locale/site#about_us 
site_faq GET /:locale/site/faq(.:format)     :locale/site#faq 
site_discounts GET /:locale/site/discounts(.:format)  :locale/site#discounts 
site_services GET /:locale/site/services(.:format)  :locale/site#services 
site_contact_us GET /:locale/site/contact_us(.:format) :locale/site#contact_us 
site_admin GET /:locale/site/admin(.:format)    :locale/site#admin 
site_posts GET /:locale/site/posts(.:format)    :locale/site#posts 
categories_new_subcategory GET /:locale/categories/new_subcategory(.:format) :locale/categories#new_subcategory 
categories_edit_subcategory GET /:locale/categories/edit_subcategory(.:format) :locale/categories#edit_subcategory 

雖然你可能能夠類似的東西,問題是應該你。如果您不熟悉它,我強烈建議閱讀面向資源的體系結構。我不會建議把Rails彎曲成奇怪的形狀,除非你對此有所瞭解。它的概念,Rails路由是基於和routes.rb發生的事情,直到你明白這一點纔有意義。

互聯網上有很多可用的內容,一本很好的書爲我澄清了一些問題,這是Leonard Richardson和Sam Ruby的Restful Web服務。 我希望有所幫助。

+0

你是對的,我有其他的路線,我已經縮小了範圍。我需要更多地瞭解寧靜的路由,我會查看那本書。我還將再次閱讀關於路由的Ruby on Rails指南。會更接受的版本是: 資源網站:然後添加一堆成員路線?我不需要我的網站控制器的CRUD,只需要靜態的操作(家庭,產品等)。這是最佳做法嗎? – ruevaughn 2012-04-04 23:35:22

+0

我已經走了一個「網站」控制器的路徑,並可以使其工作。我堅持使用Rails(而不是安裝Sinatra應用來照顧靜態頁面或類似的東西),因爲我希望這些「靜態」頁面能夠更新,我知道我將不得不在整個站點上處理I18n問題。最後,我有一個控制器代表組織的「大約」,「聯繫」和「家庭」類型的東西作爲一個單獨的組織存儲在數據庫中的屬性。其他的一切都是Rails CRUD。它的工作,但我總是懷疑是否有更好的方法。 – mikewilliamson 2012-04-05 00:07:56

+0

真棒,這正是我的情況,我也希望'靜態'頁面可以更新。但我只需要前端來切換語言,而不是後端,這就是爲什麼我只是想將範圍封裝在我的前端'靜態'頁面,如果這是有道理的。沿着這條道路走下去,就結構或其他任何你能想到的事情而言,這類網站上是否還有其他提示? – ruevaughn 2012-04-05 02:55:54

相關問題