每当我对routes.rb进行范围调整时,我的rails应用程序都会出错

时间:2012-04-06 03:55:10

标签: ruby-on-rails rails-routing

我正在尝试实现railscast中所见的国际化,每次我调整路径文件的范围时都会收到错误

 No route matches [GET] "/"

或错误

missing :controller
config/routes.rb:6:in `block (2 levels) in <top (required)>'
config/routes.rb:5:in `block in <top (required)>'
config/routes.rb:1:in `<top (required)>'

这是我的routes.rb文件

Jensenlocksmithing::Application.routes.draw do
  get "log_out" => "sessions#destroy", as: "log_out"
  get "log_in" => "sessions#new", as: "log_in"

  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"

    root :to => 'site#home'
  end

  #match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
  #match '', to: redirect("/#{I18n.default_locale}")

  match "/savesort" => 'site#savesort'

  resources :users
  resources :abouts
  resources :sessions
  resources :coupons
  resources :monthly_posts
  resources :reviews

  resources :categories do
    collection { post :sort }
      resources :children, :controller => :categories, :only => [:index, :new, :create,  :new_subcategory]
  end
  resources :products do
    member do
       put :move_up
       put :move_down
   end 
  end
  resources :faqs do
    collection { post :sort }
  end 
end

那么,为什么每当我添加范围“:locale”do end line时,我是否会收到这些错误?没有这一切都很好。如果您需要查看更多代码,请告诉我们。谢谢你们

修改

在我的应用程序控制器中,我有以下内容:

private

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

这是否与在路由中传递哈希相同?

修改2

我改变了我的路线,如本要点所示。 https://gist.github.com/2322844

那么为什么将:id部分添加到get路由中?喜欢这个

 about_us_site GET  /sites/:id/about_us(.:format)  

不应该是这样的

 about_us_site GET  /sites/about_us(.:format)

还添加了我的所有routes.rb文件及其生成的路由以获取更多信息 https://gist.github.com/2322861

回答感兴趣的人:

我改变了

    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"

    root :to => 'site#home'

 resources :sites, except: [:new, :edit, :index, :show, :update, :destroy, :create] do  
   collection do  
   get :home  
   get :about_us  
   get :faq  
   get :discounts  
   get :services  
   get :contact_us  
   get :admin  
   get :posts  
 end  

结束

1 个答案:

答案 0 :(得分:2)

传入哈希应该修复你的路线:

scope "(:locale)", :defaults => { :locale => "en" } do
  resources :sites
end

此外,您可能需要考虑创建SitesController并将其members

resources :sites do
  member do
    get :about_us # Points to /sites/about_us
  end
end