Skip to content

Commit

Permalink
Implement new product and links for new and show
Browse files Browse the repository at this point in the history
  • Loading branch information
mariochavez committed Jan 26, 2013
1 parent 6e70469 commit ec5a033
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ class ProductsController < ApplicationController
def index
@products = Product.all
end

def new
@product = Product.new active: true
end

def create
@product = Product.new params[:product]

if @product.valid?
@product.save
return redirect_to products_path, notice: "Your product #{@product.name} has been created!"
end
render :new
end
end
4 changes: 3 additions & 1 deletion app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<h1>Products list</h1>
<span><%= flash[:notice] %></span>
<p><%= link_to 'New product', new_product_path %></p>
<ul>
<% @products.each do |product| %>
<li id='<%= "product_#{product.id}" %>'>
<%= product.name %>
<%= product.name %> | <%= link_to 'Show', product %>
</li>
<% end %>
</ul>
21 changes: 21 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>New product</h1>
<% if @product.errors.any? %>
<h3>Uppss we have a problem</h3>
<% end %>
<%= form_for @product do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<span class='error'><%= @product.errors[:name].join(', ') %></span>
<br/>
<%= f.label :descritpion %>
<%= f.text_area :description, cols: 30, rows: 5 %>
<br/>
<%= f.label :price %>
<%= f.text_field :price %>
<span class='error'><%= @product.errors[:price].join(', ') %></span>
<br/>
<%= f.label :active %>
<%= f.check_box :active, checked_value: true, unchecked_value: false %>
<br/>
<%= f.submit 'Save' %>
<% end %>

0 comments on commit ec5a033

Please sign in to comment.