【问题标题】:Need to diagnose routes.rb for app to add english/spanish to Rails 3 app需要为应用程序诊断 routes.rb 以将英语/西班牙语添加到 Rails 3 应用程序
【发布时间】:2012-04-04 21:29:43
【问题描述】:

我正在关注 Ryan Bates railscasts I18n Internationalization 并遇到问题。

我正在尝试在链接中设置语言,如下所示:

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

在我看来/layouts/application.html.erb

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

现在,每当我尝试运行 rake 路由或导航到我得到的 URL 时

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

我对路线还很陌生,有人可以帮我看看/解释这个问题吗? 提前致谢。

【问题讨论】:

    标签: ruby-on-rails internationalization rails-routing


    【解决方案1】:

    我刚刚将您发布的所有代码粘贴到了一个新的 Rails 应用程序中,它可以正常工作。我的猜测是您的 routes.rb 文件中有其他路由,其中​​一个是无效的。您发布的路线是这样的:

    mike@sleepycat:~/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 中发生的事情,除非您了解这一点。

    Internet 上有很多可用的东西,一本为我澄清事情的好书是 Leonard Richardson 和 Sam Ruby 的 Restful Web services。 我希望这会有所帮助。

    【讨论】:

    • 你说得对,我还有其他路线,实际上我已经缩小了范围。我确实需要更多地了解restful routing,我会看看那本书。我还将再次阅读有关路由的 Ruby on Rails 指南。更可接受的版本是:资源站点:然后添加一堆成员路由?我的站点控制器不需要 CRUD,只需要静态的操作(家庭、产品等)。这是最好的做法吗?
    • 我之前已经走上了“站点”控制器的道路,它可以工作。我坚持使用 Rails(而不是安装 Sinatra 应用程序来处理静态页面或类似的东西),因为我希望这些“静态”页面能够更新,并且我知道我将不得不处理整个网站的 I18n 问题.最后,我有一个控制器代表组织,其中包含“关于”、“联系”和“家庭”类型的东西作为单个组织的属性,它们全部单独存储在数据库中。其他一切都是 Rails CRUD。它有效,但我总是想知道是否有更好的方法。
    • 太棒了,这正是我的情况,我还希望“静态”页面可以更新。但是我只需要前端来切换语言,而不是后端,这就是为什么我试图只将范围包裹在我的前端“静态”页面周围,如果这有意义的话。沿着这条路走下去,就结构而言,这类网站是否还有其他提示,或者您能想到的任何其他内容?
    • 我发布了一个新问题,请随意查看。 stackoverflow.com/questions/10036167/…
    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2011-12-15
    相关资源
    最近更新 更多