Permalink
Browse files

Added Droid class

rails g scaffold droid name:string
  • Loading branch information...
1 parent dda1178 commit d7893939097e2706a240849878fba9b9eb30a808 @kmayer committed Jun 21, 2011
@@ -0,0 +1,83 @@
+class DroidsController < ApplicationController
+ # GET /droids
+ # GET /droids.xml
+ def index
+ @droids = Droid.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @droids }
+ end
+ end
+
+ # GET /droids/1
+ # GET /droids/1.xml
+ def show
+ @droid = Droid.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @droid }
+ end
+ end
+
+ # GET /droids/new
+ # GET /droids/new.xml
+ def new
+ @droid = Droid.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @droid }
+ end
+ end
+
+ # GET /droids/1/edit
+ def edit
+ @droid = Droid.find(params[:id])
+ end
+
+ # POST /droids
+ # POST /droids.xml
+ def create
+ @droid = Droid.new(params[:droid])
+
+ respond_to do |format|
+ if @droid.save
+ format.html { redirect_to(@droid, :notice => 'Droid was successfully created.') }
+ format.xml { render :xml => @droid, :status => :created, :location => @droid }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @droid.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /droids/1
+ # PUT /droids/1.xml
+ def update
+ @droid = Droid.find(params[:id])
+
+ respond_to do |format|
+ if @droid.update_attributes(params[:droid])
+ format.html { redirect_to(@droid, :notice => 'Droid was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @droid.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /droids/1
+ # DELETE /droids/1.xml
+ def destroy
+ @droid = Droid.find(params[:id])
+ @droid.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(droids_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module DroidsHelper
+end
View
@@ -0,0 +1,2 @@
+class Droid < ActiveRecord::Base
+end
@@ -0,0 +1,21 @@
+<%= form_for(@droid) do |f| %>
+ <% if @droid.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@droid.errors.count, "error") %> prohibited this droid from being saved:</h2>
+
+ <ul>
+ <% @droid.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing droid</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @droid %> |
+<%= link_to 'Back', droids_path %>
@@ -0,0 +1,23 @@
+<h1>Listing droids</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @droids.each do |droid| %>
+ <tr>
+ <td><%= droid.name %></td>
+ <td><%= link_to 'Show', droid %></td>
+ <td><%= link_to 'Edit', edit_droid_path(droid) %></td>
+ <td><%= link_to 'Destroy', droid, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Droid', new_droid_path %>
@@ -0,0 +1,5 @@
+<h1>New droid</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', droids_path %>
@@ -0,0 +1,10 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @droid.name %>
+</p>
+
+
+<%= link_to 'Edit', edit_droid_path(@droid) %> |
+<%= link_to 'Back', droids_path %>
@@ -0,0 +1,13 @@
+class CreateDroids < ActiveRecord::Migration
+ def self.up
+ create_table :droids do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :droids
+ end
+end
View
@@ -0,0 +1,21 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110621153915) do
+
+ create_table "droids", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
@@ -0,0 +1,9 @@
+# This file should contain all the record creation needed to seed the database with its default values.
+# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
+#
+# Examples:
+#
+# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
+# Mayor.create(:name => 'Daley', :city => cities.first)
+
+Droid.create([{:name => 'Marvin'}, {:name => 'C3PO'}, {:name => 'R2D2'}, {:name => 'Robby'}])
View
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class DroidsControllerTest < ActionController::TestCase
+ setup do
+ @droid = droids(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:droids)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create droid" do
+ assert_difference('Droid.count') do
+ post :create, :droid => @droid.attributes
+ end
+
+ assert_redirected_to droid_path(assigns(:droid))
+ end
+
+ test "should show droid" do
+ get :show, :id => @droid.to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => @droid.to_param
+ assert_response :success
+ end
+
+ test "should update droid" do
+ put :update, :id => @droid.to_param, :droid => @droid.attributes
+ assert_redirected_to droid_path(assigns(:droid))
+ end
+
+ test "should destroy droid" do
+ assert_difference('Droid.count', -1) do
+ delete :destroy, :id => @droid.to_param
+ end
+
+ assert_redirected_to droids_path
+ end
+end
View
@@ -0,0 +1,13 @@
+ENV["RAILS_ENV"] = "test"
+require File.expand_path('../../config/environment', __FILE__)
+require 'rails/test_help'
+
+class ActiveSupport::TestCase
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
+ #
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
+ # -- they do not yet inherit this setting
+ fixtures :all
+
+ # Add more helper methods to be used by all tests here...
+end
View
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class DroidTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class DroidsHelperTest < ActionView::TestCase
+end

0 comments on commit d789393

Please sign in to comment.