2017-09-23 34 views
2

我有一个Country链接到散热片实现资源

mix phx.gen.html Location Country countries name slug:unique 

链接到一个Country我想用slug而不是id。我怎样才能实现这个代码将链接到slug(例如http://localhost:4000/countries/germany)?

<%= link "Show", to: country_path(@conn, :show, country), class: "btn btn-default btn-xs" %> 
+0

你尝试在'country_path'中使用'country.slug'吗? –

回答

3

您需要实现模块的Phoenix.Param协议。要做到这一点最简单的方法是添加你的模块中的schema "..."之前如下:

@derive {Phoenix.Param, key: :slug} 
schema "..." do 
    ... 
end 

代替id现在,从生成凤凰生成的URL链接助手时slug场将被使用。