2016-11-27 59 views
2

我想渲染我的錯誤觀點沒有佈局:無法使我的錯誤觀點,在鳳凰城/藥劑沒有佈局

defmodule MyApp.ErrorView do 
    use MyApp.Web, :view 

    def render("404.html", assigns) do # renders my_404.html.eex template 
    render(__MODULE__, "my_404.html") 
    end 

和行動:

def my_action(conn) do 
    if something_wrong do 
    conn 
    |> put_status(404) 
    |> render(MyApp.ErrorView, "404.html") 


    # conn 
    # |> put_status(404) 
    # |> put_layout(false) 
    # |> put_view(MyApp.ErrorView) 
    # |> render("404.html") 

但它不工作並呈現我的主要應用程序模板。當我取消註釋第二部分的代碼並註釋掉第一部分代碼時,它會永久掛起,並且不會呈現任何內容。

此外,我希望能夠設置layout falseErrorView,因爲我不希望有打電話給put_layout(false)從我所有的控制器中的每個動作。

+0

'conn |> put_status(404)|> put_layout(false)|> render(MyApp.ErrorView,「404.html」)'? – Dogbert

+0

@Dogbert,我會試試看。但我希望能夠將layout(false)放入我的** ErrorView **中,因爲我不想從每個操作調用'put_layout(false)'。我怎樣才能做到這一點? – Kooooro

+0

@Dogbert,是的,它可以工作,但我想這樣做 - 「我希望能夠在我的ErrorView中放置佈局(false),因爲我不想從每個操作調用put_layout(false)。我怎樣才能做到這一點? '也。 – Kooooro

回答

0

將是我會做什麼或者:

  • 加薪已經定義並具有plug_status字段設置爲404,例如異常Ecto.NoResultsError

  • 定義自己的除外模塊,做同樣的

     
    defmodule WhoopsyError do 
        defexception [:message, :plug_status] 
    
        def exception(_) do 
        %__MODULE__{message: "Whoopsy!", plug_status: 404} 
        end 
    end 
    

鳳凰將接管並渲染錯誤觀點對你沒有模板。