2012-03-19 97 views
1

我正在運行Rails 3.1.1並得到一個奇怪的錯誤。在開發中(還沒有試圖用它來推動生產),當它試圖爲新創建的記錄生成一個url時,偶爾會在我的控制器或我的郵件模板中發現路由錯誤。即使記錄已成功創建,並且與記錄屬性無關(我可以用剛好相同的參數重新創建記錄,但沒有得到錯誤,它在發生時似乎完全是隨機的)也會發生這種情況。Rails 3偶爾的路由錯誤

這似乎發生了10次之一,但我不能說我在添加郵件程序動作之前看到過它發生的事件。

還有一個可能更復雜的因素:我使用加密方法在其URL中混淆了記錄的ID,但這種方式在其他方面沒有問題。爲了做到這一點,我改編了討論的方法here

在我看來,URL並沒有及時生成link_to調用的某些時間......但這對我來說沒有多大意義。我不認爲競賽條件是我需要擔心的事情。

這裏是我的錯誤日誌,當這種情況發生在控制器(時生成的PARAMS不要求電子郵件):

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"watch_lists", :id=>#<WatchList id: 195, title: "sfdsfd", created_at: "2012-03-19 05:18:46", updated_at: "2012-03-19 05:18:46", public_list: false>}): 
    app/controllers/watch_lists_controller.rb:72:in `block (2 levels) in create' 
    app/controllers/watch_lists_controller.rb:56:in `create' 

在這裏,當它在郵件模板發生的(當PARAMS也呼籲渲染命令之前生成的電子郵件):

Rendered watch_list_mailer/share_notification.html.erb (3.2ms) 
Completed 500 Internal Server Error in 113ms 

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"watch_lists", :id=>#<WatchList id: 210, title: "sdggsgsg", created_at: "2012-03-19 05:47:17", updated_at: "2012-03-19 05:47:17", public_list: true>}): 
    20:    <% end %> 
    21:   <% end %><br> 
    22:   <br> 
    23:   Here's a link to your WatchList: <%= link_to @wl.title, watch_list_url(@wl) %><br> 
    24:   <br> 
    25:   Enjoy! 
    26:  </p> 
    app/views/watch_list_mailer/share_notification.html.erb:23:in `_app_views_watch_list_mailer_share_notification_html_erb___1391186431365383285_70156615518000' 
    app/mailers/watch_list_mailer.rb:12:in `share_notification' 
    app/controllers/watch_lists_controller.rb:124:in `share_notification' 
    app/controllers/watch_lists_controller.rb:68:in `block (2 levels) in create' 
    app/controllers/watch_lists_controller.rb:63:in `each' 
    app/controllers/watch_lists_controller.rb:63:in `block in create' 
    app/controllers/watch_lists_controller.rb:56:in `create' 

編輯:在進一步的測試,這似乎不管我是否包括郵件任務的情況發生。這似乎很可能是由鏈接混淆所引起的。有可能鏈接的編碼與它有關(我必須確保使用URI轉義它們以防止在代碼中其他位置的錯誤位置出現斜線)。我會調查這個進一步的報告。

+0

檢查'堅持?'爲您的情況下返回true。 – 2012-03-19 08:47:04

+0

我解決了它。問題在於,當加密ID包含斜線時,URL混淆會返回無效鏈接。由於記錄已成功創建,因此嘗試再次嘗試將創建具有不同ID的記錄,從而創建一個可能包含「/」或其他無效字符的不同加密URL。我之前直接在創建記錄路徑的位置進行了更正,但沒有轉到在視圖中工作。強制每個加密標識的URI編碼解決了問題。 (self.encrypt(value),Regexp.new(「[^#{URI :: PATTERN :: UNRESERVED}]」))' – 2012-03-19 18:49:43

回答

0

這是一個id加密偶爾會產生無效鏈接的問題,而且我在這個過程中沒有及時解決這個問題。

在LIB/obfuscate.rb

def uri_encrypt(value) 
    URI.escape(self.encrypt(value), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) 
end 

在我的模型

def to_param 
    uri_encrypt(id) 
end