Skip to content

Commit df3ca1c

Browse files
adds new show page with name for communities
1 parent fe4ee78 commit df3ca1c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

lib/reddit/category.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@ defmodule Reddit.Category do
101101
def change_community(%Community{} = community, attrs \\ %{}) do
102102
Community.changeset(community, attrs)
103103
end
104+
105+
106+
def get_name(param) do
107+
Repo.get_by(Community, name: param)
108+
end
104109
end

lib/reddit_web/controllers/community_controller.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ defmodule RedditWeb.CommunityController do
2626
end
2727
end
2828

29-
def show(conn, %{"id" => id}) do
30-
community = Category.get_community!(id)
31-
render(conn, "show.html", community: community)
29+
def show(conn, %{"name" => name}) do
30+
community = Category.get_name(name)
31+
32+
if community do
33+
render(conn, "show.html", community: community)
34+
else
35+
redirect(conn, to: "/")
36+
end
3237
end
3338

3439
def edit(conn, %{"id" => id}) do

lib/reddit_web/templates/community/index.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<td><%= community.rules %></td>
1919

2020
<td>
21-
<span><%= link "Show", to: Routes.community_path(@conn, :show, community) %></span>
21+
<span><%= link "Show", to: Routes.community_path(@conn, :show, community.name) %></span>
2222
<span><%= link "Edit", to: Routes.community_path(@conn, :edit, community) %></span>
2323
<span><%= link "Delete", to: Routes.community_path(@conn, :delete, community), method: :delete, data: [confirm: "Are you sure?"] %></span>
2424
</td>

0 commit comments

Comments
 (0)