rue*_*ghn 3 ruby-on-rails rails-routing
我之前提过这个问题并认为它是固定的,但事实并非如此.先前的问题在这里
我的问题是我试图设置我的路线,以便在我输入时
本地主机: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}")
Run Code Online (Sandbox Code Playgroud)
但截至目前,它重定向到/ en/en/en/en/en/en/en/en/en/en/sites/admin(添加en直到浏览器抱怨).
有什么想法,它一直在添加/ en?
编辑: 答案很棒,谢谢.你能帮我诊断根路线吗?
root to: redirect("#{/#{I18n.default_locale}") # handles /
Run Code Online (Sandbox Code Playgroud)
我知道重定向正在寻找类似的东西
redirect("www.example.com")
Run Code Online (Sandbox Code Playgroud)
这就离开了这一部分
#{/#{I18n.default_locale}
Run Code Online (Sandbox Code Playgroud)
#{正在使用rubys字符串插值,对吗?我不确定那是怎么回事.
那么我们就有了
/#{I18n.default_locale}
Run Code Online (Sandbox Code Playgroud)
哪个也使用字符串插值并打印出I18n.default_locale的值?
希望这是有道理的,我真的很感激帮助,我正在学习很多东西.
编辑2:
我改变了这条线
root to: redirect("#{/#{I18n.default_locale}") # handles /
Run Code Online (Sandbox Code Playgroud)
至
root to: redirect("/#{I18n.default_locale}") # handles /
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否正确.现在我收到了错误
uninitialized constant LocaleController
Run Code Online (Sandbox Code Playgroud)
我知道它从根到"locale #root"的错误,但我认为语言环境#将来自范围.
我会继续玩它,让你知道任何进展.
这是我的路线文件https://gist.github.com/2332198的新链接
Mic*_*ley 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
Run Code Online (Sandbox Code Playgroud)