Commit 7faf76f

mo khan <mo@mokhan.ca>
2013-06-22 15:55:01
create needs scaffold
1 parent a33c1bf
app/assets/javascripts/needs.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
app/assets/stylesheets/needs.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the needs controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,69 @@
+body {
+  background-color: #fff;
+  color: #333;
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size: 13px;
+  line-height: 18px;
+}
+
+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;
+  &:visited {
+    color: #666;
+  }
+  &:hover {
+    color: #fff;
+    background-color: #000;
+  }
+}
+
+div {
+  &.field, &.actions {
+    margin-bottom: 10px;
+  }
+}
+
+#notice {
+  color: green;
+}
+
+.field_with_errors {
+  padding: 2px;
+  background-color: red;
+  display: table;
+}
+
+#error_explanation {
+  width: 450px;
+  border: 2px solid red;
+  padding: 7px;
+  padding-bottom: 0;
+  margin-bottom: 20px;
+  background-color: #f0f0f0;
+  h2 {
+    text-align: left;
+    font-weight: bold;
+    padding: 5px 5px 5px 15px;
+    font-size: 12px;
+    margin: -7px;
+    margin-bottom: 0px;
+    background-color: #c00;
+    color: #fff;
+  }
+  ul li {
+    font-size: 12px;
+    list-style: square;
+  }
+}
app/controllers/needs_controller.rb
@@ -0,0 +1,83 @@
+class NeedsController < ApplicationController
+  # GET /needs
+  # GET /needs.json
+  def index
+    @needs = Need.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json { render json: @needs }
+    end
+  end
+
+  # GET /needs/1
+  # GET /needs/1.json
+  def show
+    @need = Need.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render json: @need }
+    end
+  end
+
+  # GET /needs/new
+  # GET /needs/new.json
+  def new
+    @need = Need.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.json { render json: @need }
+    end
+  end
+
+  # GET /needs/1/edit
+  def edit
+    @need = Need.find(params[:id])
+  end
+
+  # POST /needs
+  # POST /needs.json
+  def create
+    @need = Need.new(params[:need])
+
+    respond_to do |format|
+      if @need.save
+        format.html { redirect_to @need, notice: 'Need was successfully created.' }
+        format.json { render json: @need, status: :created, location: @need }
+      else
+        format.html { render action: "new" }
+        format.json { render json: @need.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /needs/1
+  # PUT /needs/1.json
+  def update
+    @need = Need.find(params[:id])
+
+    respond_to do |format|
+      if @need.update_attributes(params[:need])
+        format.html { redirect_to @need, notice: 'Need was successfully updated.' }
+        format.json { head :no_content }
+      else
+        format.html { render action: "edit" }
+        format.json { render json: @need.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /needs/1
+  # DELETE /needs/1.json
+  def destroy
+    @need = Need.find(params[:id])
+    @need.destroy
+
+    respond_to do |format|
+      format.html { redirect_to needs_url }
+      format.json { head :no_content }
+    end
+  end
+end
app/helpers/needs_helper.rb
@@ -0,0 +1,2 @@
+module NeedsHelper
+end
app/models/need.rb
@@ -0,0 +1,3 @@
+class Need < ActiveRecord::Base
+  attr_accessible :description, :user_id
+end
app/views/needs/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@need) do |f| %>
+  <% if @need.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@need.errors.count, "error") %> prohibited this need from being saved:</h2>
+
+      <ul>
+      <% @need.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :description %><br />
+    <%= f.text_area :description %>
+  </div>
+  <div class="field">
+    <%= f.label :user_id %><br />
+    <%= f.number_field :user_id %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/needs/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing need</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @need %> |
+<%= link_to 'Back', needs_path %>
app/views/needs/index.html.erb
@@ -0,0 +1,25 @@
+<h1>Listing needs</h1>
+
+<table>
+  <tr>
+    <th>Description</th>
+    <th>User</th>
+    <th></th>
+    <th></th>
+    <th></th>
+  </tr>
+
+<% @needs.each do |need| %>
+  <tr>
+    <td><%= need.description %></td>
+    <td><%= need.user_id %></td>
+    <td><%= link_to 'Show', need %></td>
+    <td><%= link_to 'Edit', edit_need_path(need) %></td>
+    <td><%= link_to 'Destroy', need, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+  </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Need', new_need_path %>
app/views/needs/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New need</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', needs_path %>
app/views/needs/show.html.erb
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Description:</b>
+  <%= @need.description %>
+</p>
+
+<p>
+  <b>User:</b>
+  <%= @need.user_id %>
+</p>
+
+
+<%= link_to 'Edit', edit_need_path(@need) %> |
+<%= link_to 'Back', needs_path %>
config/routes.rb
@@ -48,7 +48,8 @@ Yycrebuild::Application.routes.draw do
 
   # You can have the root of your site routed with "root"
   # just remember to delete public/index.html.
-  # root :to => 'welcome#index'
+  resources :needs
+  root :to => 'needs#index'
 
   # See how all your routes lay out with "rake routes"
 
db/migrate/20130622155342_create_needs.rb
@@ -0,0 +1,10 @@
+class CreateNeeds < ActiveRecord::Migration
+  def change
+    create_table :needs do |t|
+      t.text :description
+      t.integer :user_id
+
+      t.timestamps
+    end
+  end
+end
db/schema.rb
@@ -11,6 +11,13 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 0) do
+ActiveRecord::Schema.define(:version => 20130622155342) do
+
+  create_table "needs", :force => true do |t|
+    t.text     "description"
+    t.integer  "user_id"
+    t.datetime "created_at",  :null => false
+    t.datetime "updated_at",  :null => false
+  end
 
 end