2013-01-23 70 views
2

在一個地方有沒有一個很好的解決方案來實現Rails中的這個目標?(最好是routes.rb)。現在重定向我濾波器之前做了一個這樣的:在Rails中重定向所有URL(已知和未知)

... 
unless [temp_url].include? request.url 
    redirect_to temp_path 
end 

這種方法適用於已知的路線。未知的路線將得到404錯誤。對於未知有可能在routes.rb使用此:

match "/*other" => redirect("/temp/index") 

顯然,我們不必request對象routes.rb訪問。是否有更好的解決方案來覆蓋routes.rb中的已知和未知重定向?

+0

我想說這兩個單獨的功能就夠了。需要「全面通用」的案例非常少見,因此爲它們增加一行額外功能應該是可行的。 – Smar

回答

0

有在before_filter仔細一看:

例如,下面的代碼總是調用顯示,編輯,更新find_post方法和破壞端點。你的情況可以使用相同的邏輯。

class PostsController < ApplicationController 
    before_filter :find_post, :only => [:show, :edit, :update, :destroy] 

    def update 
    @post.update_attributes(params[:post]) 
    end 

    def destroy 
    @post.destroy 
    end 

    protected  
    def find_post 
     @post = current_user.posts.find(params[:id]) 
    end 
end 
2

未知路由重定向到根

routes.rb

match '*path' => redirect('/') 

使用上面你可以重定向所有未知路線的根源。