2017-05-03 35 views
1

我有一個'收音機'的模型,並在我的主頁(template/page/index.html.eex)我正在拉最後的錄音。這可以正常工作,但我很努力讓鏈接工作到特定記錄的顯示頁面(template/radio/show.html.eex)不能得到菲尼克斯連接路線上班

在控制檯中我得到:

== Compilation error on file web/controllers/radio_controller.ex == 
** (CompileError) web/controllers/radio_controller.ex:31: undefined function last_radio/0 

上線很多例子使用@conn:

<%= link "Show", to: radio_path(@conn, :show, radio), class: "button" %>

我一直在努力,這個轉變讓我的last_radio查詢:

<%= link "Show", to: radio_path(@last_radio, :show, radio), class: "button" %>

但這並不奏效。

也不:

<%= link "Show", to: radio_path(@conn, :show, last_radio), class: "button" %>

radio_controller

defmodule Radios.RadioController do 
    use Radios.Web, :controller 

    alias Radios.Radio 

    def index(conn, _params) do 
    radios = Repo.all(Radio) 
    render conn, "index.html" 
    end 

    def show(conn, %{"id" => id}) do 
    radio = Repo.get!(Radio, id) 
    render(conn, "show.html", radio: radio) 
    end 

page_controller

defmodule Radios.PageController do 
    use Radios.Web, :controller 

    alias Radios.Radio 


    def index(conn, _params) do 
    last_radio = Radio |> last |> Repo.one 
    #|> Radio.sorted 
    #|> Radios.Repo.one 
    render(conn, "index.html", last_radio: last_radio) 

    end 
end 

什麼是I D錯了嗎?

+0

錯誤在第31行,但是,您提供的代碼顯示的行少於31行。控制器是否比這更長? –

+0

這是的,但你的答案已經修復了。當然是 –

回答

3

如果要在模板中使用控制器中的綁定,則需要以@爲前綴。因此,在您templates/radio/文件夾使用鏈接:

<%= link "Show", to: radio_path(@conn, :show, @radio), class: "button" %> 

templates/page使用:

<%= link "Show", to: radio_path(@conn, :show, @last_radio), class: "button" %> 

附加說明:

如果您呈現從模板的部分,從主叫綁定模板不要得到傳遞給部分。你需要自己處理它們。我添加這個,這樣可以爲您節省一些時間。

<%= render "my_partial", conn: @conn, radio: @radio %> 
+1

。現在非常有意義!完全有效。 –