重定向传统路由会使用rake呈现映射器错误

时间:2011-06-04 16:53:12

标签: ruby-on-rails ruby-on-rails-3 redirect routes rake

routes.rb中:

Myapp::Application.routes.draw do
  root :to => "posts#index"
  match "posts/autocomplete_topic_name", :as => "autocomplete_topic_name"
  match "/new" => "posts#new", :as => :new
  # resources
  resources :topics
  resources :posts
  resources :comments
  # static
  match "/about"   => "pages#about",   :as => :about
  match "/help"    => "pages#help",    :as => :help
  match "/home"    => "home#index",    :as => :home
  # redirects
  match "/tags" => redirect("/topics")
  match "/entries" => redirect("/posts")
  match "/comments" => redirect("/")
end

rake routes结束时,错误显示为:

              tags        /tags(.:format)                          {:to=>#<Proc:0x00000001485d18@/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>}
            entries        /entries(.:format)                       {:to=>#<Proc:0x0000000133d438@/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>}
                           /comments(.:format)                      {:to=>#<Proc:0x000000012e8708@/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>}

我正在尝试将一些传统路线重定向到新路线以获得更好的搜索引擎优化。路线如:

/entries/8需要重定向到/posts/8等等。

2 个答案:

答案 0 :(得分:1)

坚持使用application_controller.rb

rescue_from ActionController::RoutingError do |exception|
  flash[:error] = "Sorry, we were not quite sure where you were trying to go"
  redirect_to root_url
end

答案 1 :(得分:0)

您需要保留完整的网址并将其与任何参数一起传递。在这种情况下,即使是外卡​​匹配也不会,因为您无法将其传递给新路线。

试试这个

match "/entries/:post_id" => redirect("/posts/%{post_id}")

这将传递您的参数以及抛出正确的HTTP错误代码,以便谷歌更新它的索引。