Last active
March 7, 2017 21:15
-
-
Save thebugcatcher/d257b7d311f962f9651636a1a8442182 to your computer and use it in GitHub Desktop.
Add category_name to products index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Listing products</h2> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Price</th> | |
<th>Category</th> | |
<th></th> | |
</tr> | |
</thead> | |
<tbody> | |
<%= for product <- @products do %> | |
<tr> | |
<td><%= product.name %></td> | |
<td><%= product.price %></td> | |
<td><%= product.category.category_name %></td> <!-- Update this line --> | |
<td class="text-right"> | |
<%= link "Show", to: product_path(@conn, :show, product), class: "btn btn-default btn-xs" %> | |
<%= link "Edit", to: product_path(@conn, :edit, product), class: "btn btn-default btn-xs" %> | |
<%= link "Delete", to: product_path(@conn, :delete, product), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %> | |
</td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
<%= link "New product", to: product_path(@conn, :new) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RummageExample.ProductController do | |
use RummageExample.Web, :controller | |
alias RummageExample.Product | |
def index(conn, _params) do | |
products = Product | |
|> Repo.all | |
|> Repo.preload(:category) # <-- Add this | |
render(conn, "index.html", products: products) | |
end | |
def new(conn, _params) do | |
changeset = Product.changeset(%Product{}) | |
render(conn, "new.html", changeset: changeset) | |
end | |
def create(conn, %{"product" => product_params}) do | |
changeset = Product.changeset(%Product{}, product_params) | |
case Repo.insert(changeset) do | |
{:ok, _product} -> | |
conn | |
|> put_flash(:info, "Product created successfully.") | |
|> redirect(to: product_path(conn, :index)) | |
{:error, changeset} -> | |
render(conn, "new.html", changeset: changeset) | |
end | |
end | |
def show(conn, %{"id" => id}) do | |
product = Repo.get!(Product, id) | |
render(conn, "show.html", product: product) | |
end | |
def edit(conn, %{"id" => id}) do | |
product = Repo.get!(Product, id) | |
changeset = Product.changeset(product) | |
render(conn, "edit.html", product: product, changeset: changeset) | |
end | |
def update(conn, %{"id" => id, "product" => product_params}) do | |
product = Repo.get!(Product, id) | |
changeset = Product.changeset(product, product_params) | |
case Repo.update(changeset) do | |
{:ok, product} -> | |
conn | |
|> put_flash(:info, "Product updated successfully.") | |
|> redirect(to: product_path(conn, :show, product)) | |
{:error, changeset} -> | |
render(conn, "edit.html", product: product, changeset: changeset) | |
end | |
end | |
def delete(conn, %{"id" => id}) do | |
product = Repo.get!(Product, id) | |
# Here we use delete! (with a bang) because we expect | |
# it to always work (and if it does not, it will raise). | |
Repo.delete!(product) | |
conn | |
|> put_flash(:info, "Product deleted successfully.") | |
|> redirect(to: product_path(conn, :index)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment