Ruby on Rails - 重定向旧路由会导致映射器错误



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)>}

我正在尝试将一些旧路由重定向到新路由以获得更好的 SEO。路线如:

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

把它

贴在你的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

您需要保留完整的 url 并将其与任何参数一起传递。在这种情况下,即使是外卡匹配也无法进行,因为您将无法将其传递到新路由。

试试这个

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

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

相关内容

  • 没有找到相关文章

最新更新