2011-09-05 97 views
0

我只是從Rails 3中的大型應用程序升級到Rails的3.1的中間,我的執行頁面控制器的敲了一個問題:的Rails 3.1和靜態頁面

when templates doesnt exist 
    should render the 404 page (FAILED - 1) 

Failures: 

    1) PagesController automatic paths when templates doesnt exist should render the 404 page 
    Failure/Error: get 'base_page_processor', :base_page => 'something_that_doesnt_exist' 
    NoMethodError: 
     undefined method `map' for "pages":String 
    # ./app/controllers/pages_controller.rb:5:in `base_page_processor' 
    # ./spec/controllers/pages_controller_spec.rb:37:in `block (3 levels) in <top (required)>' 

Finished in 0.10557 seconds 
4 examples, 1 failure 

Failed examples: 

rspec ./spec/controllers/pages_controller_spec.rb:36 # PagesController automatic paths when templates doesnt exist should render the 404 page 

這Rails有工作3.0。必須通過template_exists方法更改某些內容。這裏是控制器:

class PagesController < ApplicationController 
    def base_page_processor 
    view_prefix = "pages" 

    if params[:base_page].present? && template_exists?(params[:base_page], view_prefix) 
     render "#{view_prefix}/#{params[:base_page]}" 
    else 
     #TODO : Notify missing url via email error or error notification service 
     render '/public/404.html', :status => 404 
    end 
    end 
end 

解決方案代碼:

class PagesController < ApplicationController 
    def base_page_processor 
    view_prefix = ["pages"] 

    if params[:base_page].present? && template_exists?(params[:base_page], view_prefix) 
     render "#{view_prefix[0]}/#{params[:base_page]}" 
    else 
     #TODO : Notify missing url via email error or error notification service 
     render '/errors/404.html', :status => 404 
    end 
    end 
end 

我也注意到,它並沒有渲染錯誤的觀點(即:/public/404.html),所以我創建了一個應用程序目錄/ views/errors並把所有的錯誤靜態頁面放在那裏,現在就渲染它們。有用。

謝謝安德魯。

回答

1

template_exists方法參數指示第二個參數prefix應該是一個數組。通常Rails方法通過將某些東西轉換爲數組來接受這兩種方法,所以這有點不尋常。

exists?(name, prefixes = [], partial = false, keys = []) 

這種方法也是別名爲template_exists?

# File actionpack/lib/action_view/lookup_context.rb, line 93 
def exists?(name, prefixes = [], partial = false, keys = []) 
    @view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys)) 
end 

那麼製作view_prefix = ["pages"]應該有效嗎? (並相應地修改剩餘的字符串插值)