Skip to content

Commit

Permalink
Scaffold Products, Comments
Browse files Browse the repository at this point in the history
feature Products
  • Loading branch information
pjfitzgibbons committed Feb 5, 2009
1 parent 35d8062 commit 5ad2836
Show file tree
Hide file tree
Showing 38 changed files with 1,150 additions and 0 deletions.
85 changes: 85 additions & 0 deletions app/controllers/comments_controller.rb
@@ -0,0 +1,85 @@
class CommentsController < ApplicationController
# GET /comments
# GET /comments.xml
def index
@comments = Comment.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @comments }
end
end

# GET /comments/1
# GET /comments/1.xml
def show
@comment = Comment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @comment }
end
end

# GET /comments/new
# GET /comments/new.xml
def new
@comment = Comment.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @comment }
end
end

# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end

# POST /comments
# POST /comments.xml
def create
@comment = Comment.new(params[:comment])

respond_to do |format|
if @comment.save
flash[:notice] = 'Comment was successfully created.'
format.html { redirect_to(@comment) }
format.xml { render :xml => @comment, :status => :created, :location => @comment }
else
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end

# PUT /comments/1
# PUT /comments/1.xml
def update
@comment = Comment.find(params[:id])

respond_to do |format|
if @comment.update_attributes(params[:comment])
flash[:notice] = 'Comment was successfully updated.'
format.html { redirect_to(@comment) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /comments/1
# DELETE /comments/1.xml
def destroy
@comment = Comment.find(params[:id])
@comment.destroy

respond_to do |format|
format.html { redirect_to(comments_url) }
format.xml { head :ok }
end
end
end
85 changes: 85 additions & 0 deletions app/controllers/products_controller.rb
@@ -0,0 +1,85 @@
class ProductsController < ApplicationController
# GET /products
# GET /products.xml
def index
@products = Product.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end

# GET /products/1
# GET /products/1.xml
def show
@product = Product.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end

# GET /products/new
# GET /products/new.xml
def new
@product = Product.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end

# GET /products/1/edit
def edit
@product = Product.find(params[:id])
end

# POST /products
# POST /products.xml
def create
@product = Product.new(params[:product])

respond_to do |format|
if @product.save
flash[:notice] = 'Product was successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end

# PUT /products/1
# PUT /products/1.xml
def update
@product = Product.find(params[:id])

respond_to do |format|
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@product) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /products/1
# DELETE /products/1.xml
def destroy
@product = Product.find(params[:id])
@product.destroy

respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
module CommentsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/products_helper.rb
@@ -0,0 +1,2 @@
module ProductsHelper
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
@@ -0,0 +1,2 @@
class Comment < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions app/models/product.rb
@@ -0,0 +1,2 @@
class Product < ActiveRecord::Base
end
20 changes: 20 additions & 0 deletions app/views/comments/edit.html.erb
@@ -0,0 +1,20 @@
<h1>Editing comment</h1>

<% form_for(@comment) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>
22 changes: 22 additions & 0 deletions app/views/comments/index.html.erb
@@ -0,0 +1,22 @@
<h1>Listing comments</h1>

<table>
<tr>
<th>Name</th>
<th>Body</th>
</tr>

<% for comment in @comments %>
<tr>
<td><%=h comment.name %></td>
<td><%=h comment.body %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New comment', new_comment_path %>
19 changes: 19 additions & 0 deletions app/views/comments/new.html.erb
@@ -0,0 +1,19 @@
<h1>New comment</h1>

<% form_for(@comment) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', comments_path %>
13 changes: 13 additions & 0 deletions app/views/comments/show.html.erb
@@ -0,0 +1,13 @@
<p>
<b>Name:</b>
<%=h @comment.name %>
</p>

<p>
<b>Body:</b>
<%=h @comment.body %>
</p>


<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
20 changes: 20 additions & 0 deletions app/views/products/edit.html.erb
@@ -0,0 +1,20 @@
<h1>Editing product</h1>

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

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
22 changes: 22 additions & 0 deletions app/views/products/index.html.erb
@@ -0,0 +1,22 @@
<h1>Listing products</h1>

<table>
<tr>
<th>Name</th>
<th>Price</th>
</tr>

<% for product in @products %>
<tr>
<td><%=h product.name %></td>
<td><%=h product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>
19 changes: 19 additions & 0 deletions app/views/products/new.html.erb
@@ -0,0 +1,19 @@
<h1>New product</h1>

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

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
13 changes: 13 additions & 0 deletions app/views/products/show.html.erb
@@ -0,0 +1,13 @@
<p>
<b>Name:</b>
<%=h @product.name %>
</p>

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


<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
4 changes: 4 additions & 0 deletions config/routes.rb
@@ -1,4 +1,8 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :comments

map.resources :products

# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.


# Sample of regular route: # Sample of regular route:
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20090129031627_create_products.rb
@@ -0,0 +1,14 @@
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.decimal :price, :precision => 12, :scale => 2

t.timestamps
end
end

def self.down
drop_table :products
end
end
14 changes: 14 additions & 0 deletions db/migrate/20090129031654_create_comments.rb
@@ -0,0 +1,14 @@
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.string :name
t.text :body

t.timestamps
end
end

def self.down
drop_table :comments
end
end

0 comments on commit 5ad2836

Please sign in to comment.