Skip to content
Browse files

admin scaffolding for adding/editing items

  • Loading branch information...
1 parent eb28e2c commit 8b880686b8e0b2ec891946434b089d8e01b0a46d @jsjohnst committed
View
85 app/controllers/admin/items_controller.rb
@@ -0,0 +1,85 @@
+class Admin::ItemsController < ApplicationController
+ # GET /items
+ # GET /items.xml
+ def index
+ @items = Item.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @items }
+ end
+ end
+
+ # GET /items/1
+ # GET /items/1.xml
+ def show
+ @item = Item.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @item }
+ end
+ end
+
+ # GET /items/new
+ # GET /items/new.xml
+ def new
+ @item = Item.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @item }
+ end
+ end
+
+ # GET /items/1/edit
+ def edit
+ @item = Item.find(params[:id])
+ end
+
+ # POST /items
+ # POST /items.xml
+ def create
+ @item = Item.new(params[:item])
+
+ respond_to do |format|
+ if @item.save
+ flash[:notice] = 'Item was successfully created.'
+ format.html { redirect_to([:admin, @item]) }
+ format.xml { render :xml => @item, :status => :created, :location => @item }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @item.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /items/1
+ # PUT /items/1.xml
+ def update
+ @item = Item.find(params[:id])
+
+ respond_to do |format|
+ if @item.update_attributes(params[:item])
+ flash[:notice] = 'Item was successfully updated.'
+ format.html { redirect_to([:admin, @item]) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @item.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /items/1
+ # DELETE /items/1.xml
+ def destroy
+ @item = Item.find(params[:id])
+ @item.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(admin_items_url) }
+ format.xml { head :ok }
+ end
+ end
+end
View
2 app/helpers/admin/items_helper.rb
@@ -0,0 +1,2 @@
+module Admin::ItemsHelper
+end
View
52 app/views/admin/items/edit.html.erb
@@ -0,0 +1,52 @@
+<h1>Editing item</h1>
+
+<% form_for([:admin, @item]) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :isbn %><br />
+ <%= f.text_field :isbn %>
+ </p>
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description, :cols => 40, :rows => 15 %>
+ <p>
+ <%= f.label :author %><br />
+ <%= f.text_field :author %>
+ </p>
+ <p>
+ <%= f.label :publisher %><br />
+ <%= f.text_field :publisher %>
+ </p>
+ <p>
+ <%= f.label :source %><br />
+ <%= f.text_field :source %>
+ </p>
+ <p>
+ <%= f.label :owner %><br />
+ <%= f.text_field :owner %>
+ </p>
+ <p>
+ <%= f.label :image_url %><br />
+ <%= f.text_field :image_url %>
+ </p>
+ <p>
+ <%= f.label :available %><br />
+ Yes:&nbsp;<%= f.radio_button :available, true %>&nbsp;No:&nbsp;<%= f.radio_button :available, false %>
+ </p>
+ <p>
+ <%= f.label :electronic %><br />
+ Yes:&nbsp;<%= f.radio_button :electronic, true %>&nbsp;No:&nbsp;<%= f.radio_button :electronic, false %>
+ </p>
+
+ <p>
+ <%= f.submit 'Update' %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', [:admin, @item] %> |
+<%= link_to 'Back', admin_items_path %>
View
19 app/views/admin/items/index.html.erb
@@ -0,0 +1,19 @@
+<h1>Listing items</h1>
+
+<table>
+ <tr>
+ </tr>
+
+<% @items.each do |item| %>
+ <tr>
+ <td><%=h item.title %></td>
+ <td><%= link_to 'Show', [:admin, item] %></td>
+ <td><%= link_to 'Edit', edit_admin_item_path(item) %></td>
+ <td><%= link_to 'Destroy', [:admin, item], :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New item', new_admin_item_path %>
View
51 app/views/admin/items/new.html.erb
@@ -0,0 +1,51 @@
+<h1>New item</h1>
+
+<% form_for([:admin, @item]) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :isbn %><br />
+ <%= f.text_field :isbn %>
+ </p>
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description, :cols => 40, :rows => 15 %>
+ <p>
+ <%= f.label :author %><br />
+ <%= f.text_field :author %>
+ </p>
+ <p>
+ <%= f.label :publisher %><br />
+ <%= f.text_field :publisher %>
+ </p>
+ <p>
+ <%= f.label :source %><br />
+ <%= f.text_field :source %>
+ </p>
+ <p>
+ <%= f.label :owner %><br />
+ <%= f.text_field :owner %>
+ </p>
+ <p>
+ <%= f.label :image_url %><br />
+ <%= f.text_field :image_url %>
+ </p>
+ <p>
+ <%= f.label :available %><br />
+ Yes:&nbsp;<%= f.radio_button :available, true %>&nbsp;No:&nbsp;<%= f.radio_button :available, false %>
+ </p>
+ <p>
+ <%= f.label :electronic %><br />
+ Yes:&nbsp;<%= f.radio_button :electronic, true %>&nbsp;No:&nbsp;<%= f.radio_button :electronic, false %>
+ </p>
+
+ <p>
+ <%= f.submit 'Create' %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', admin_items_path %>
View
44 app/views/admin/items/show.html.erb
@@ -0,0 +1,44 @@
+<p>
+ <b>ISBN:</b>
+ <%=h @item.isbn %>
+</p>
+<p>
+ <b>Title:</b>
+ <%=h @item.title %>
+</p>
+<p>
+ <b>Description:</b>
+ <%=h @item.description %>
+</p>
+<p>
+ <b>Author:</b>
+ <%=h @item.author %>
+</p>
+<p>
+ <b>Publisher:</b>
+ <%=h @item.publisher %>
+</p>
+<p>
+ <b>Source:</b>
+ <%=h @item.source %>
+</p>
+<p>
+ <b>Owner:</b>
+ <%=h @item.owner %>
+</p>
+<p>
+ <b>Image URL:</b>
+ <%=h @item.image_url %>
+</p>
+<p>
+ <b>Electronic:</b>
+ <%= @item.electronic ? "Yes" : "No" %>
+</p>
+<p>
+ <b>Available:</b>
+ <%= @item.available ? "Yes" : "No" %>
+</p>
+
+
+<%= link_to 'Edit', edit_admin_item_path(@item) %> |
+<%= link_to 'Back', admin_items_path %>
View
17 app/views/admin/layouts/items.html.erb
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Items: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
View
8 config/routes.rb
@@ -24,11 +24,9 @@
# products.resources :sales, :collection => { :recent => :get }
# end
- # Sample resource route within a namespace:
- # map.namespace :admin do |admin|
- # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
- # admin.resources :products
- # end
+ map.namespace :admin do |admin|
+ admin.resources :items
+ end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "library"
View
54 public/stylesheets/scaffold.css
@@ -0,0 +1,54 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+.fieldWithErrors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#errorExplanation {
+ width: 400px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#errorExplanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#errorExplanation p {
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
+}
+
+#errorExplanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
+
View
45 test/functional/admin/items_controller_test.rb
@@ -0,0 +1,45 @@
+require 'test_helper'
+
+class Admin::ItemsControllerTest < ActionController::TestCase
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:items)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create item" do
+ assert_difference('Item.count') do
+ post :create, :item => { }
+ end
+
+ assert_redirected_to admin_item_path(assigns(:item))
+ end
+
+ test "should show item" do
+ get :show, :id => items(:one).to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => items(:one).to_param
+ assert_response :success
+ end
+
+ test "should update item" do
+ put :update, :id => items(:one).to_param, :item => { }
+ assert_redirected_to admin_item_path(assigns(:item))
+ end
+
+ test "should destroy item" do
+ assert_difference('Item.count', -1) do
+ delete :destroy, :id => items(:one).to_param
+ end
+
+ assert_redirected_to admin_items_path
+ end
+end
View
4 test/unit/helpers/admin/items_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class Admin::ItemsHelperTest < ActionView::TestCase
+end

0 comments on commit 8b88068

Please sign in to comment.
Something went wrong with that request. Please try again.