为什么 Rails 应用程序意外重定向而不是匹配路线? [英] Why rails app is redirecting unexpectedly instead of matching the route?
问题描述
我之前问过这个问题并认为它已修复,但事实并非如此.上一个问题这里
I asked this question earlier and thought it was fixed, but it's not. Previous question here
我的问题是我试图设置我的路线,以便在我输入时
My problem is I am trying to set my routes so that when I type in
本地主机:3000/sites/admin
localhost:3000/sites/admin
它应该重定向到
本地主机:3000/en/sites/admin
localhost:3000/en/sites/admin
这是我的 routes.rb 文件
here is my routes.rb file
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 直到浏览器抱怨).
But as of right now, it redirects to /en/en/en/en/en/en/en/en/en/en/sites/admin (adds en until browser complains).
有什么想法为什么它不断添加/en?
Any thoughts why it keeps adding /en?
答案很好,谢谢.你能帮我诊断根路由吗?
The answer is great, thanks. Can you help me diagnose the root route?
root to: redirect("#{/#{I18n.default_locale}") # handles /
我知道重定向正在寻找类似的东西
I know redirects is looking for something like
redirect("www.example.com")
所以剩下这部分
#{/#{I18n.default_locale}
#{ 使用的是 rubys 字符串插值,对吗?我不确定那 { 在做什么.
The #{ is using rubys string interpolation, right? i'm not sure what that { is doing though.
那么我们有
/#{I18n.default_locale}
哪个也使用字符串插值并打印出 I18n.default_locale 的值?
Which is also using string interpolation and to print out the value of I18n.default_locale?
希望这是有道理的,我真的非常感谢您的帮助,我学到了很多东西.
Hopefully that makes sense, I really really appreciate the help, I am learning a lot.
编辑 2:
我从
root to: redirect("#{/#{I18n.default_locale}") # handles /
到
root to: redirect("/#{I18n.default_locale}") # handles /
但我不确定这是否正确.现在我收到错误
But i'm not sure if thats right. Now i'm getting the error
uninitialized constant LocaleController
我知道错误是从 root 到:locale#root",但我认为 locale# 会来自作用域.
I know it's getting the error from the root to: "locale#root", but i thought the locale# would come from the scope.
我会继续玩它,让你知道任何进展.
I'll continue playing with it and let you know any progress.
这是我的路由文件的新链接https://gist.github.com/2332198
Here is a new link to my routes file https://gist.github.com/2332198
推荐答案
我们又见面了,ruevaughn.:)
We meet again, ruevaughn. :)
我创建了一个测试 Rails 应用程序,以下最小示例对我有用:
I created a test rails app and the following minimal example works for me:
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
这篇关于为什么 Rails 应用程序意外重定向而不是匹配路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!