Permalink
Browse files

added book creating functionality

  • Loading branch information...
1 parent 958c2a9 commit d4f3fb5de30721a8cbb713076934a583d2ea2a15 Michael committed Dec 17, 2011
View
3 app/assets/javascripts/book.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/
View
3 app/assets/javascripts/books.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/
View
3 app/assets/stylesheets/books.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Books controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
31 app/controllers/books_controller.rb
@@ -0,0 +1,31 @@
+class BooksController < ApplicationController
+ def new
+ @title = "Add book"
+ @book = Book.new
+ end
+
+ def create
+ book = current_user.books.create!(:title => params[:book][:title],
+ :author => params[:book][:author])
+ if book.nil?
+ flash.now[:error] = "Book could not be created."
+ @title = "Add book"
+ render 'new'
+ else
+ flash.now[:success] = "Book Created!"
+ redirect_to book
+ end
+ end
+
+ def show
+ @book = Book.find(params[:id])
+ @title = @book.title
+ end
+
+ def destroy
+ Book.find(params[:id]).destroy
+ flash[:success] = "Book Deleted."
+ redirect_to book_path
+ end
+
+end
View
1 app/controllers/users_controller.rb
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
+ @book = Book.new
end
def signup
View
2 app/helpers/books_helper.rb
@@ -0,0 +1,2 @@
+module BooksHelper
+end
View
5 app/models/book.rb
@@ -1,6 +1,7 @@
-require 'digest'
class Book < ActiveRecord::Base
- attr_accessor :title, :author, :uid
+ belongs_to :user
+ attr_accessible :title, :author
+
validates :title, :presence => true,
:length => { :maximum => 50 },
View
2 app/models/user.rb
@@ -1,5 +1,7 @@
require 'digest'
class User < ActiveRecord::Base
+ has_many :books
+
attr_accessor :password
attr_accessible :username, :first, :last, :password, :password_confirmation
View
18 app/views/books/_addbook.html.erb
@@ -0,0 +1,18 @@
+<p>
+ <%= form_for@book do |f| %>
+ <div class="field">
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </div>
+
+ <div class="field">
+ <%= f.label :author %><br />
+ <%= f.text_field :author %>
+ </div>
+
+ <div class="actions">
+ <%= f.submit "Add book" %>
+ </div>
+
+ <% end %>
+</p>
View
3 app/views/books/new.html.erb
@@ -0,0 +1,3 @@
+<h1>Create Book</h1>
+
+<%= render 'addbook' %>
View
3 app/views/books/show.html.erb
@@ -0,0 +1,3 @@
+<p>
+ This is the book: <%= @book.name %>
+</p>
View
27 app/views/users/show.html.erb
@@ -3,24 +3,19 @@
</h1>
<p>
- This is the user <%= @user.name %>
+ This is the user <%= @user.name %>. <br>
+ He has <%= @user.books.count %> books. <br>
+
+ Here they are
</p>
<p>
- <%= form_for(@book) do |f| %>
- <div class="field">
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </div>
-
- <div class="field">
- <%= f.label :author %><br />
- <%= f.text_field :author %>
- </div>
+ This is params: <%= @param %>
+
+ <% @user.books.each do |b| %>
+ <br><%= b.name %> <br>
+ <% end %>
+</p>
- <div class="actions">
- <%= f.submit "Add book" %>
- </div>
- <% end %>
-</p>
+<%= render 'books/addbook' %>
View
6 config/routes.rb
@@ -1,13 +1,17 @@
BookmarkZone::Application.routes.draw do
- #get "sessions/new"
+
resources :users
resources :sessions, :only => [:new, :create, :destroy]
+ resources :books, :only => [:new, :create, :destroy, :show]
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/signup', :to => 'users#signup'
+
+ match '/addbook', :to => 'books#new'
+ match '/removebook', :to => 'books#destroy'
root :to => 'pages#home'
View
10 db/migrate/20111217122904_create_books.rb
@@ -0,0 +1,10 @@
+class CreateBooks < ActiveRecord::Migration
+ def change
+ create_table :books do |t|
+ t.string :title
+ t.string :author
+
+ t.timestamps
+ end
+ end
+end
View
11 db/migrate/20111217133317_add_user_id_to_books.rb
@@ -0,0 +1,11 @@
+class AddUserIdToBooks < ActiveRecord::Migration
+ def self.up
+ add_column :books, :user_id, :integer
+ add_index :books, [:user_id, :created_at]
+ end
+
+ def self.down
+ remove_index :books, :user_id
+ remove_column :books, :user_id
+ end
+end
View
13 db/schema.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
# 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.
@@ -10,7 +11,17 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111216225610) do
+ActiveRecord::Schema.define(:version => 20111217133317) do
+
+ create_table "books", :force => true do |t|
+ t.string "title"
+ t.string "author"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.integer "user_id"
+ end
+
+ add_index "books", ["user_id", "created_at"], :name => "index_books_on_user_id_and_created_at"
create_table "users", :force => true do |t|
t.string "username"
View
9 test/fixtures/books.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ title: MyString
+ author: MyString
+
+two:
+ title: MyString
+ author: MyString
View
9 test/functional/book_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class BookControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+end
View
9 test/functional/books_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class BooksControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+end
View
7 test/unit/book_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class BookTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/book_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class BookHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/books_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class BooksHelperTest < ActionView::TestCase
+end

0 comments on commit d4f3fb5

Please sign in to comment.