Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
class ProductsController < ApplicationController
def destroy
@product = Product.where(:id => params[:id]).first
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
def update
@product = Product.where(:id => params[:id]).first
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to @product, :notice => 'Product was succesfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit"}
format.json { render json: @product.errors, :status => :unprocessable_entity }
end
end
end
def edit
@product = Product.where(:id => params[:id]).first
end
def show
@product = Product.where(:id => params[:id]).first
end
def new
@product = Product.new
end
def index
@products = Product.includes(:user).all
respond_to do |format|
format.html
format.json {render :json => @products}
end
end
def create
@product = Product.new(params[:product])

respond_to do |format|
if @product.save
format.html {render :action => 'create'}
format.json {render :json => @product}
else
format.html {render :action => 'new'}
format.json {render :json => @product.errors, :status => :unprocessable_entity}
end
end
end
end
6 changes: 5 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
</head>
<body>

<%= link_to "a list of users", users_path %> |
<%= link_to "a list of users", users_path %> |
<%= link_to "a list of products", products_path %>

<% if flash[:notice].present? %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>

<%= yield %>

</body>
Expand Down
27 changes: 27 additions & 0 deletions app/views/products/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

<%= form_for(@product) do |f| %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="actoins">
<%= f.submit %>
</div>
<% end %>

<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
21 changes: 2 additions & 19 deletions app/views/products/create.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
<h2>Create View</h2>

<%= params[:product].inspect %>



<% product = Product.new(params[:product]) %>
<%= product.save %>
<%= product.inspect %>

<br />
<% product.errors.inspect %>

<% if product.save %>
<h2> Congrats You Created a New Product</h2>
Your product looks like <%= product.inspect %>
<% else %>
<h2>Your product was not saved!! </h2>
<%= product.errors.full_messages %>
Please go back in your browser and fix the problem
<% end %>
<h2>Product Created Successfully<h2>
<% @product.name %> added to the website, it costs: $<%= @product.price %>
5 changes: 5 additions & 0 deletions app/views/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Editing Product</h1>

<%= render 'form' %>

<% link_to 'Back', products_path %>
9 changes: 4 additions & 5 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<h2> Products in the market </h2>
<% lots_of_products = Product.includes(:user).all %>

<ul>
<% lots_of_products.each do |product| %>
<% @products.each do |product| %>
<li>
Product Name: "<%= product.name %>" costs $<%= product.price %> |
Sold by <%= product.user.name if product.user.present? %>
Product Name: "<%= product.name %>" costs $<%= product.price %> |
Sold by <%= product.user.name if product.user.present? %>
</li>
<% end %>
</ul>
</ul>
13 changes: 1 addition & 12 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
<h2>Let's add new products!</h2>

<form method='post' action='/products' >
<label>Name of your product</label><br>
<input name="product[name]" size="30" type="text">

<br />
<label>Price</label><br>
<input name="product[price]" type="number">

<br />
<input type="submit" value="Create Product">
</form>

<%= render :partial => 'products/form'%>
12 changes: 12 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<p>
<b>Name:</b>
<%= @product.name %>
</p>

<p>
<b>Price:</b>
<%= @product.price %>
</p>

<%= link_to 'Destroy', @product, :method => :delete, :data => { :confirm => 'Are you sure?' } %>
5 changes: 1 addition & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
ControllerExercise::Application.routes.draw do


get '/products' => 'products#index'
get '/products/new' => 'products#new'
post '/products' => 'products#create'

resources :products
resources :users

# The priority is based upon order of creation:
Expand Down