为什么rails应用程序意外重定向而不是匹配路由?

时间:2012-04-07 05:50:45

标签: ruby-on-rails rails-routing

我之前问了这个问题并认为它已修复,但事实并非如此。上一个问题here

我的问题是我正在尝试设置我的路线,以便在我输入

本地主机:3000 /位点/管理

应该重定向到

本地主机:3000 / EN /位点/管理

这是我的routes.rb文件

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
  get "log_out" => "sessions#destroy", as: "log_out"
  get "log_in" => "sessions#new", as: "log_in"

  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
end
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 
root :to => 'sites#home'
match "/savesort" => 'sites#savesort'

end

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

但截至目前,它已重定向到/ en / en / en / en / en / en / en / en / en / en / sites / admin(在浏览器抱怨之前添加en)。

为什么它一直在添加/ en?

修改 答案很棒,谢谢。你能帮我诊断根路线吗?

root to: redirect("#{/#{I18n.default_locale}") # handles /

我知道重定向正在寻找像

这样的东西
redirect("www.example.com")

这样就离开了这部分

#{/#{I18n.default_locale}

#{正在使用rubys字符串插值,对吗?我不确定那是怎么回事。

那么我们就有了

/#{I18n.default_locale}

哪个也使用字符串插值并打印出I18n.default_locale的值?

希望这是有道理的,我真的很感激帮助,我正在学习很多。

编辑2:

我从

改变了这一行
root to: redirect("#{/#{I18n.default_locale}") # handles /

root to: redirect("/#{I18n.default_locale}") # handles /

但我不确定这是否正确。现在我收到了错误

uninitialized constant LocaleController

我知道它从根目录收到错误:“locale #root”,但我认为语言环境#将来自范围。

我会继续玩它,让你知道任何进展。

以下是我的路线档案https://gist.github.com/2332198

的新链接

2 个答案:

答案 0 :(得分:10)

我们再见面,ruevaughn。 :)

我创建了一个测试rails应用程序,以下最小的示例适用于我:

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
  resources :sites do
    collection do
      get :admin
    end
  end

  root to: "locale#root" # handles /en/
  match "*path", to: "locale#not_found" # handles /en/fake/path/whatever
end

root to: redirect("/#{I18n.default_locale}") # handles /
match '*path', to: redirect("/#{I18n.default_locale}/%{path}") # handles /not-a-locale/anything

答案 1 :(得分:0)

使用Rails 4.0.x时,重定向中的%{path}将转义路径中的斜杠,因此您将获得无限循环重定向到/en/en%2Fen%2Fen%2Fen...

以防有人像我一样正在寻找适合Rails-4的解决方案,这就是我发现没有问题的工作,即使有更复杂的路径需要重定向:

# Redirect root to /:locale if needed
root to: redirect("/#{I18n.locale}", status: 301)

# Match missing locale paths to /:locale/path
# handling additional formats and/or get params
match '*path', to: (redirect(status: 307) do |params,request|
  sub_params = request.params.except :path
  if sub_params.size > 0
    format = sub_params[:format]
    sub_params.except! :format
    if format.present?
      "/#{I18n.locale}/#{params[:path]}.#{format}?#{sub_params.to_query}"
    else
      "/#{I18n.locale}/#{params[:path]}?#{sub_params.to_query}"
    end
  else
    "/#{I18n.locale}/#{params[:path]}"
  end
end), via: :all

# Redirect to custom root if all previous matches fail
match '', to: redirect("/#{I18n.locale}", status: 301), via: :all