From aadc09c7066c6fbb7e83af04f232e80ff6f45e27 Mon Sep 17 00:00:00 2001 From: Ross Greene Date: Thu, 10 Sep 2020 13:31:18 +0100 Subject: [PATCH 1/5] non-working attempt to add copy functionality --- .idea/workspace.xml | 92 +++++++++++++------ .../{copy.coffee => copies.coffee} | 0 .../stylesheets/{copy.scss => copies.scss} | 2 +- app/controllers/books_controller.rb | 10 +- app/controllers/copies_controller.rb | 50 ++++++++++ app/controllers/copy_controller.rb | 2 - app/helpers/copies_helper.rb | 2 + app/helpers/copy_helper.rb | 2 - app/views/books/index.html.erb | 8 +- app/views/copies/index.html.erb | 24 +++++ app/views/copies/new.html.erb | 15 +++ config/environments/production.rb | 2 +- config/initializers/inflections.rb | 4 +- config/routes.rb | 2 +- db/migrate/20200909101937_create_copies.rb | 4 +- 15 files changed, 168 insertions(+), 51 deletions(-) rename app/assets/javascripts/{copy.coffee => copies.coffee} (100%) rename app/assets/stylesheets/{copy.scss => copies.scss} (64%) create mode 100755 app/controllers/copies_controller.rb delete mode 100755 app/controllers/copy_controller.rb create mode 100755 app/helpers/copies_helper.rb delete mode 100755 app/helpers/copy_helper.rb create mode 100755 app/views/copies/index.html.erb create mode 100755 app/views/copies/new.html.erb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4f83dbd..11fadbc 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,6 +11,9 @@ + + + @@ -27,11 +30,11 @@ - - + + - - + + @@ -42,6 +45,10 @@ + + + + @@ -270,7 +277,7 @@ @@ -293,54 +300,54 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + @@ -362,4 +369,31 @@ + + + + + file://$PROJECT_DIR$/app/controllers/copies_controller.rb + 18 + + + + + file://$PROJECT_DIR$/app/controllers/copies_controller.rb + 13 + + + + + + + + + + + \ No newline at end of file diff --git a/app/assets/javascripts/copy.coffee b/app/assets/javascripts/copies.coffee similarity index 100% rename from app/assets/javascripts/copy.coffee rename to app/assets/javascripts/copies.coffee diff --git a/app/assets/stylesheets/copy.scss b/app/assets/stylesheets/copies.scss similarity index 64% rename from app/assets/stylesheets/copy.scss rename to app/assets/stylesheets/copies.scss index 9539dc6..3a74ec5 100755 --- a/app/assets/stylesheets/copy.scss +++ b/app/assets/stylesheets/copies.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the copy controller here. +// Place all the styles related to the copies controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 66dbf59..82ec954 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -3,10 +3,6 @@ def index @books = Book.all end - def show - @book = Book.find(params[:id]) - end - def new @book = Book.new end @@ -32,17 +28,13 @@ def edit def update @book = Book.find(params[:id]) - if @book.update_attributes(book_param) + if @book.update_attributes(book_params) redirect_to action: 'index' else render action: 'edit' end end - def book_param - params.require(:book).permit(:title, :price, :subject_id, :description) - end - # I was expecting to call this 'delete' since that is what 'method' calls # Why is it destroy instead? Is there any way I could have guessed that? def destroy diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb new file mode 100755 index 0000000..390074a --- /dev/null +++ b/app/controllers/copies_controller.rb @@ -0,0 +1,50 @@ +class CopiesController < ApplicationController + def index + @book = Book.find(params[:id]) + @copies = Copy.find_by_book_id(@book.id) + end + + def show; end + + def new + @copy = Copy.new + # @book = Book.find(params[:id]) + # @copies.instance_variable_set(:book, @book.id) + end + + def create + @copy = Copy.new(copy_params) + + if @copy.save + redirect_to action: 'index' + else + puts 'Did not create' + render action: 'new' + end + end + + def copy_params + params.require(:copy).permit(:borrower, :due_date, :book) + end + + def edit + @copy = Copy.find(params[:id]) + end + + def update + @copy = Copy.find(params[:id]) + + if @copy.update_attributes(copy_params) + redirect_to action: 'index' + else + render action: 'edit' + end + end + + # I was expecting to call this 'delete' since that is what 'method' calls + # Why is it destroy instead? Is there any way I could have guessed that? + def destroy + Book.find(params[:id]).destroy + redirect_to action: 'list' + end +end diff --git a/app/controllers/copy_controller.rb b/app/controllers/copy_controller.rb deleted file mode 100755 index b37704c..0000000 --- a/app/controllers/copy_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class CopyController < ApplicationController -end diff --git a/app/helpers/copies_helper.rb b/app/helpers/copies_helper.rb new file mode 100755 index 0000000..735e977 --- /dev/null +++ b/app/helpers/copies_helper.rb @@ -0,0 +1,2 @@ +module CopiesHelper +end diff --git a/app/helpers/copy_helper.rb b/app/helpers/copy_helper.rb deleted file mode 100755 index 18368a7..0000000 --- a/app/helpers/copy_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module CopyHelper -end diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb index 6c9d431..7e427e2 100644 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -5,9 +5,11 @@

All Books

    <% @books.each { |book| %> -
  • <%= book.title %> - (<%= link_to "View & edit", :action => 'edit', :id => book %>)
  • -
  • <%= button_to "delete", book, :method => :delete %>
  • +
  • <%= book.title %>
  • + <%= button_to "delete", book, :method => :delete %> + <%= link_to "Edit", :action => 'edit', :id => book%> + <%= link_to "View Copies", {:controller => :copies, :action => 'index', :id => book} %> +

    <% } %>
diff --git a/app/views/copies/index.html.erb b/app/views/copies/index.html.erb new file mode 100755 index 0000000..4be1c5b --- /dev/null +++ b/app/views/copies/index.html.erb @@ -0,0 +1,24 @@ +<% if @copies.blank? %> +

There are no copies of this book in the library

+ +<% else %> +

All Copies of <% @book.title %>

+ + + + + + <% @copies.each { |copy| %> + + + + + <% } %> +
BorrowerReturn Date
<%= copy.Borrower %><%= copy.DueDate %>
+<%end %> + +

<%= link_to "Add new Copy", {:action => 'new', id: @book }%>

+ +
+ +<%= link_to 'Back', {:controller => :books, :action => 'index'} %> \ No newline at end of file diff --git a/app/views/copies/new.html.erb b/app/views/copies/new.html.erb new file mode 100755 index 0000000..aa36c22 --- /dev/null +++ b/app/views/copies/new.html.erb @@ -0,0 +1,15 @@ +

Add new Copy

+ +<%= form_with model: @copy do |f| %> +

<%= f.label :borrower %>: + <%= f.text_field :borrower %>

+ +

<%= f.label :due_date %>: + <%= f.date_field :due_date %>

+ + <%= f.submit "Create" %> +<% end -%> + +
+ +<%= link_to 'Back', {:action => :index} %> \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index 71feec7..64b20fa 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -6,7 +6,7 @@ # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers - # and those relying on copy on write to perform better. + # and those relying on copies on write to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index ac033bf..756c25b 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -1,5 +1,7 @@ # Be sure to restart your server when you modify this file. - +ActiveSupport::Inflector.inflections do |inflect| + inflect.irregular 'copies', 'copies' +end # Add new inflection rules using the following format. Inflections # are locale specific, and you may define rules for as many different # locales as you wish. All of these examples are active by default: diff --git a/config/routes.rb b/config/routes.rb index f47f3aa..3e3d22b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ Rails.application.routes.draw do - resources :books + resources :books, :copies # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20200909101937_create_copies.rb b/db/migrate/20200909101937_create_copies.rb index bd5f4f0..82b26e7 100644 --- a/db/migrate/20200909101937_create_copies.rb +++ b/db/migrate/20200909101937_create_copies.rb @@ -1,8 +1,8 @@ class CreateCopies < ActiveRecord::Migration[5.2] def change create_table :copies do |t| - t.string :Borrower, null: false - t.date :DueDate, null: false + t.string :borrower, null: false + t.date :due_date, null: false t.timestamps end From 51cb0af0eff2c4d39b34933a09747d480cae8c99 Mon Sep 17 00:00:00 2001 From: Ross Greene Date: Fri, 11 Sep 2020 10:09:03 +0100 Subject: [PATCH 2/5] Can add and view copies --- .idea/workspace.xml | 109 ++++++++++----------- app/controllers/books_controller.rb | 7 +- app/controllers/copies_controller.rb | 22 ++--- app/helpers/books_helper.rb | 0 app/models/book.rb | 6 ++ app/models/copy.rb | 3 + app/views/books/_book_form.html.erb | 12 +++ app/views/books/edit.html.erb | 19 +--- app/views/books/index.html.erb | 8 +- app/views/books/new.html.erb | 18 +--- app/views/books/show.html.erb | 10 ++ app/views/copies/index.html.erb | 26 ++--- app/views/copies/new.html.erb | 5 +- config/environments/production.rb | 0 config/initializers/inflections.rb | 2 +- config/routes.rb | 7 +- db/migrate/20200909101937_create_copies.rb | 0 db/schema.rb | 4 +- 18 files changed, 122 insertions(+), 136 deletions(-) mode change 100644 => 100755 app/controllers/books_controller.rb mode change 100644 => 100755 app/helpers/books_helper.rb create mode 100755 app/views/books/_book_form.html.erb mode change 100644 => 100755 app/views/books/index.html.erb create mode 100755 app/views/books/show.html.erb mode change 100644 => 100755 config/environments/production.rb mode change 100644 => 100755 config/initializers/inflections.rb mode change 100644 => 100755 config/routes.rb mode change 100644 => 100755 db/migrate/20200909101937_create_copies.rb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 11fadbc..2d24f6d 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,9 +11,8 @@ - - - + + @@ -30,14 +29,18 @@ - - + + - - + + + + + + @@ -45,11 +48,10 @@ - - + + + + + + - + @@ -277,7 +284,7 @@ @@ -300,54 +307,62 @@
- + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + - - + + - - + + - + @@ -360,36 +375,16 @@ - + - + - - - - file://$PROJECT_DIR$/app/controllers/copies_controller.rb - 18 - - - - - file://$PROJECT_DIR$/app/controllers/copies_controller.rb - 13 - - - - - diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb old mode 100644 new mode 100755 index 82ec954..21a85f1 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -7,6 +7,10 @@ def new @book = Book.new end + def show + @book = Book.find(params[:id]) + end + def create @book = Book.new(book_params) @@ -35,10 +39,9 @@ def update end end - # I was expecting to call this 'delete' since that is what 'method' calls - # Why is it destroy instead? Is there any way I could have guessed that? def destroy Book.find(params[:id]).destroy redirect_to action: 'index' end end + diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb index 390074a..ea2e108 100755 --- a/app/controllers/copies_controller.rb +++ b/app/controllers/copies_controller.rb @@ -1,19 +1,17 @@ class CopiesController < ApplicationController def index - @book = Book.find(params[:id]) - @copies = Copy.find_by_book_id(@book.id) + @book = Book.find(params[:book_id]) + @copies = @book.copies end - def show; end - def new - @copy = Copy.new - # @book = Book.find(params[:id]) - # @copies.instance_variable_set(:book, @book.id) + @book = Book.find(params[:book_id]) + @copy = @book.copies.new end def create - @copy = Copy.new(copy_params) + @book = Book.find(params[:book_id]) + @copy = @book.copies.new(copy_params) if @copy.save redirect_to action: 'index' @@ -24,7 +22,7 @@ def create end def copy_params - params.require(:copy).permit(:borrower, :due_date, :book) + params.require(:copy).permit(:borrower, :due_date) end def edit @@ -41,10 +39,8 @@ def update end end - # I was expecting to call this 'delete' since that is what 'method' calls - # Why is it destroy instead? Is there any way I could have guessed that? def destroy - Book.find(params[:id]).destroy - redirect_to action: 'list' + Copy.find(params[:id]).destroy + redirect_to action: 'index' end end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb old mode 100644 new mode 100755 diff --git a/app/models/book.rb b/app/models/book.rb index 03d56b4..ec0086c 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -1,2 +1,8 @@ class Book < ApplicationRecord + has_many :copies, dependent: :destroy + + # TODO: handle better if validation fails + validates :title, presence: true + validates :author, presence: true + validates :isbn, presence: true end diff --git a/app/models/copy.rb b/app/models/copy.rb index 6e2f4d1..4a1a0ac 100644 --- a/app/models/copy.rb +++ b/app/models/copy.rb @@ -1,2 +1,5 @@ class Copy < ApplicationRecord + belongs_to :book + validates :borrower, presence: true + validates :due_date, presence: true end diff --git a/app/views/books/_book_form.html.erb b/app/views/books/_book_form.html.erb new file mode 100755 index 0000000..75d8bc1 --- /dev/null +++ b/app/views/books/_book_form.html.erb @@ -0,0 +1,12 @@ +<%= form_with model: @book do |f| %> + + <% %w[title author isbn].each do |input| %> +

<%= f.label input %>: + <%= f.text_field input %>

+ <% end %> + + <%= f.submit %> +
+ + <%= link_to 'Return to all Books', {:action => 'index'} %> +<% end -%> \ No newline at end of file diff --git a/app/views/books/edit.html.erb b/app/views/books/edit.html.erb index b83a92d..f5daca0 100755 --- a/app/views/books/edit.html.erb +++ b/app/views/books/edit.html.erb @@ -1,18 +1 @@ -

Add new book

- -<%= form_with model: @book do |f| %> -

<%= f.label :title %>: - <%= f.text_field :title %>

- -

<%= f.label :author %>: - <%= f.text_field :author %>

- -

<%= f.label :isbn %>: - <%= f.text_field :isbn %>

- - <%= f.submit "Change" %> -<% end -%> - -
- -<%= link_to 'Back', {:action => 'index'} %> \ No newline at end of file +<%= render 'book_form', :object => @book %> diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb old mode 100644 new mode 100755 index 7e427e2..ff76cb5 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -5,16 +5,12 @@

All Books

    <% @books.each { |book| %> -
  • <%= book.title %>
  • - <%= button_to "delete", book, :method => :delete %> - <%= link_to "Edit", :action => 'edit', :id => book%> - <%= link_to "View Copies", {:controller => :copies, :action => 'index', :id => book} %> -
    +
  • <%= link_to book.title, book_path(book) %>

  • <% } %>
<%end %> -

<%= link_to "Add new Book", {:action => 'new' }%>

+

<%= link_to "Add new Book", :action => :new %>

diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb index ed96693..4f4f122 100755 --- a/app/views/books/new.html.erb +++ b/app/views/books/new.html.erb @@ -1,18 +1,2 @@ -

Add new book

+<%= render 'book_form', :object => @book %> -<%= form_with model: @book do |f| %> -

<%= f.label :title %>: - <%= f.text_field :title %>

- -

<%= f.label :author %>: - <%= f.text_field :author %>

- -

<%= f.label :isbn %>: - <%= f.text_field :isbn %>

- - <%= f.submit "Create" %> -<% end -%> - -
- -<%= link_to 'Back', {:action => 'index'} %> diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb new file mode 100755 index 0000000..4f4885a --- /dev/null +++ b/app/views/books/show.html.erb @@ -0,0 +1,10 @@ +

<%= @book.title %>

+

By <%= @book.author %>

+

ISBN: <%= @book.isbn %>

+ +<%= link_to "Edit", :action => 'edit' %>

+<%= button_to "delete", :method => :delete %> + +

<%= link_to "View Copies", book_copies_path(@book) %>

+ +<%= link_to 'Back to all books', books_path %> \ No newline at end of file diff --git a/app/views/copies/index.html.erb b/app/views/copies/index.html.erb index 4be1c5b..c56f4f1 100755 --- a/app/views/copies/index.html.erb +++ b/app/views/copies/index.html.erb @@ -2,23 +2,17 @@

There are no copies of this book in the library

<% else %> -

All Copies of <% @book.title %>

- - - - - - <% @copies.each { |copy| %> - - - - - <% } %> -
BorrowerReturn Date
<%= copy.Borrower %><%= copy.DueDate %>
+

All Copies of <%= @book.title %>

+ + <% @copies.each { |copy| %> +

Borrowed by <%=copy.borrower %> with a return date of + <%=copy.due_date %>

+ + <% } %> + <%end %> -

<%= link_to "Add new Copy", {:action => 'new', id: @book }%>

+

<%= link_to "Add new Copy", :action => 'new' %>


- -<%= link_to 'Back', {:controller => :books, :action => 'index'} %> \ No newline at end of file +<%= link_to 'Back', book_path(@book) %> \ No newline at end of file diff --git a/app/views/copies/new.html.erb b/app/views/copies/new.html.erb index aa36c22..ded637e 100755 --- a/app/views/copies/new.html.erb +++ b/app/views/copies/new.html.erb @@ -1,6 +1,7 @@

Add new Copy

-<%= form_with model: @copy do |f| %> +<%= form_with model: [@book, @copy] do |f| %> +

<%= f.label :borrower %>: <%= f.text_field :borrower %>

@@ -12,4 +13,4 @@
-<%= link_to 'Back', {:action => :index} %> \ No newline at end of file +<%= link_to 'Back', :action => :index %> \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb old mode 100644 new mode 100755 diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb old mode 100644 new mode 100755 index 756c25b..9f616c2 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -1,6 +1,6 @@ # Be sure to restart your server when you modify this file. ActiveSupport::Inflector.inflections do |inflect| - inflect.irregular 'copies', 'copies' + inflect.irregular 'copy', 'copies' end # Add new inflection rules using the following format. Inflections # are locale specific, and you may define rules for as many different diff --git a/config/routes.rb b/config/routes.rb old mode 100644 new mode 100755 index 3e3d22b..e65a60b --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do - resources :books, :copies - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + resources :books do + resources :copies, shallow: true + end end +# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + diff --git a/db/migrate/20200909101937_create_copies.rb b/db/migrate/20200909101937_create_copies.rb old mode 100644 new mode 100755 diff --git a/db/schema.rb b/db/schema.rb index cac5dbb..cede1ce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -21,8 +21,8 @@ end create_table "copies", force: :cascade do |t| - t.string "Borrower", null: false - t.date "DueDate", null: false + t.string "borrower", null: false + t.date "due_date", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "book_id" From 2fba415c763ec013d2f4553431722426ca06994c Mon Sep 17 00:00:00 2001 From: Ross Greene Date: Fri, 11 Sep 2020 11:24:32 +0100 Subject: [PATCH 3/5] Tidied book form partial --- .idea/workspace.xml | 64 ++++++++++++++-------------- app/controllers/copies_controller.rb | 5 +++ app/views/books/_book_form.html.erb | 3 +- app/views/books/new.html.erb | 1 - app/views/copies/_copy_form.html.erb | 16 +++++++ app/views/copies/edit.html.erb | 4 ++ app/views/copies/index.html.erb | 7 ++- app/views/copies/new.html.erb | 15 +------ 8 files changed, 67 insertions(+), 48 deletions(-) create mode 100755 app/views/copies/_copy_form.html.erb create mode 100755 app/views/copies/edit.html.erb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2d24f6d..ca57843 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,8 +11,8 @@
- - + + @@ -33,12 +33,13 @@ + - - + + @@ -48,10 +49,11 @@ + + - - + @@ -284,7 +286,7 @@ @@ -307,62 +309,62 @@
- + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb index ea2e108..75415bb 100755 --- a/app/controllers/copies_controller.rb +++ b/app/controllers/copies_controller.rb @@ -1,4 +1,8 @@ class CopiesController < ApplicationController + before_action :find_book + + def find_book; end + def index @book = Book.find(params[:book_id]) @copies = @book.copies @@ -43,4 +47,5 @@ def destroy Copy.find(params[:id]).destroy redirect_to action: 'index' end + private end diff --git a/app/views/books/_book_form.html.erb b/app/views/books/_book_form.html.erb index 75d8bc1..bfa066d 100755 --- a/app/views/books/_book_form.html.erb +++ b/app/views/books/_book_form.html.erb @@ -6,7 +6,8 @@ <% end %> <%= f.submit %> +
- <%= link_to 'Return to all Books', {:action => 'index'} %> + <%= link_to 'Return', @book.id ? book_path(@book) : books_path %> <% end -%> \ No newline at end of file diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb index 4f4f122..f5daca0 100755 --- a/app/views/books/new.html.erb +++ b/app/views/books/new.html.erb @@ -1,2 +1 @@ <%= render 'book_form', :object => @book %> - diff --git a/app/views/copies/_copy_form.html.erb b/app/views/copies/_copy_form.html.erb new file mode 100755 index 0000000..1f240e0 --- /dev/null +++ b/app/views/copies/_copy_form.html.erb @@ -0,0 +1,16 @@ +<%= form_with model: [@book, @copy] do |f| %> + +

<%= f.label :borrower %>: + <%= f.text_field :borrower %>

+ +

+ <%= f.label :due_date %>: + <%= f.date_field :due_date %> +

+ + <%= f.submit %> + +
+ + <%= link_to 'Return to all Books', :return_url %> +<% end -%> \ No newline at end of file diff --git a/app/views/copies/edit.html.erb b/app/views/copies/edit.html.erb new file mode 100755 index 0000000..5a1cc3a --- /dev/null +++ b/app/views/copies/edit.html.erb @@ -0,0 +1,4 @@ +<%= render 'copy_form' %> + +
+<%= link_to 'Back', book_path(@copy.book_id) %> \ No newline at end of file diff --git a/app/views/copies/index.html.erb b/app/views/copies/index.html.erb index c56f4f1..23e27f5 100755 --- a/app/views/copies/index.html.erb +++ b/app/views/copies/index.html.erb @@ -5,8 +5,11 @@

All Copies of <%= @book.title %>

<% @copies.each { |copy| %> -

Borrowed by <%=copy.borrower %> with a return date of - <%=copy.due_date %>

+

+ Borrowed by <%=copy.borrower %> + with a return date of <%=copy.due_date %> + (<%= link_to 'edit', edit_copy_path(copy), :action => :edit %>) +

<% } %> diff --git a/app/views/copies/new.html.erb b/app/views/copies/new.html.erb index ded637e..e50a9b7 100755 --- a/app/views/copies/new.html.erb +++ b/app/views/copies/new.html.erb @@ -1,16 +1,5 @@ -

Add new Copy

- -<%= form_with model: [@book, @copy] do |f| %> - -

<%= f.label :borrower %>: - <%= f.text_field :borrower %>

- -

<%= f.label :due_date %>: - <%= f.date_field :due_date %>

- - <%= f.submit "Create" %> -<% end -%> +<%= render 'copy_form' %>
-<%= link_to 'Back', :action => :index %> \ No newline at end of file +<%= link_to 'Back', book_path(@book) %> \ No newline at end of file From a5b7a0b6fc0c7f355c473e9bf13b67197f53c96b Mon Sep 17 00:00:00 2001 From: Ross Greene Date: Fri, 11 Sep 2020 11:42:49 +0100 Subject: [PATCH 4/5] Can edit copies --- .idea/workspace.xml | 66 ++++++++++++++++------------ app/controllers/copies_controller.rb | 4 +- app/views/books/_book_form.html.erb | 2 +- app/views/books/new.html.erb | 2 +- app/views/copies/_copy_form.html.erb | 5 ++- app/views/copies/edit.html.erb | 5 +-- app/views/copies/new.html.erb | 4 -- 7 files changed, 47 insertions(+), 41 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ca57843..26e8649 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,8 +11,6 @@ - - @@ -40,6 +38,8 @@ + + @@ -286,7 +286,7 @@ @@ -309,62 +309,62 @@
- + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + @@ -387,6 +387,18 @@ + + + + file://$PROJECT_DIR$/app/controllers/copies_controller.rb + 40 + + + + + diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb index 75415bb..fca8fbf 100755 --- a/app/controllers/copies_controller.rb +++ b/app/controllers/copies_controller.rb @@ -34,10 +34,11 @@ def edit end def update + @copy = Copy.find(params[:id]) if @copy.update_attributes(copy_params) - redirect_to action: 'index' + redirect_to book_copies_path(@copy.book_id) else render action: 'edit' end @@ -47,5 +48,4 @@ def destroy Copy.find(params[:id]).destroy redirect_to action: 'index' end - private end diff --git a/app/views/books/_book_form.html.erb b/app/views/books/_book_form.html.erb index bfa066d..5a9d610 100755 --- a/app/views/books/_book_form.html.erb +++ b/app/views/books/_book_form.html.erb @@ -9,5 +9,5 @@
- <%= link_to 'Return', @book.id ? book_path(@book) : books_path %> + <%= link_to 'Return', @book.id ? book_path(@book) : book_path %> <% end -%> \ No newline at end of file diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb index f5daca0..774a15a 100755 --- a/app/views/books/new.html.erb +++ b/app/views/books/new.html.erb @@ -1 +1 @@ -<%= render 'book_form', :object => @book %> +<%= render 'book_form' %> diff --git a/app/views/copies/_copy_form.html.erb b/app/views/copies/_copy_form.html.erb index 1f240e0..8979542 100755 --- a/app/views/copies/_copy_form.html.erb +++ b/app/views/copies/_copy_form.html.erb @@ -10,7 +10,8 @@ <%= f.submit %> -
+

+ + <%= link_to 'Back', book_copies_path(@copy.book_id || @book) %> - <%= link_to 'Return to all Books', :return_url %> <% end -%> \ No newline at end of file diff --git a/app/views/copies/edit.html.erb b/app/views/copies/edit.html.erb index 5a1cc3a..cc1408e 100755 --- a/app/views/copies/edit.html.erb +++ b/app/views/copies/edit.html.erb @@ -1,4 +1 @@ -<%= render 'copy_form' %> - -
-<%= link_to 'Back', book_path(@copy.book_id) %> \ No newline at end of file +<%= render 'copy_form' %> \ No newline at end of file diff --git a/app/views/copies/new.html.erb b/app/views/copies/new.html.erb index e50a9b7..9f09f1f 100755 --- a/app/views/copies/new.html.erb +++ b/app/views/copies/new.html.erb @@ -1,5 +1 @@ <%= render 'copy_form' %> - -
- -<%= link_to 'Back', book_path(@book) %> \ No newline at end of file From e9cde03e76a314a444ed3512bc3404cd94fdf1b5 Mon Sep 17 00:00:00 2001 From: Ross Greene Date: Fri, 11 Sep 2020 12:16:50 +0100 Subject: [PATCH 5/5] Added before_action to copies_controller --- .idea/workspace.xml | 54 ++++++++++++++-------------- app/controllers/copies_controller.rb | 23 ++++++------ app/views/books/_book_form.html.erb | 2 +- app/views/copies/_copy_form.html.erb | 3 +- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 26e8649..b6aeb78 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -286,7 +286,7 @@
@@ -309,62 +309,62 @@
- + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + @@ -391,11 +391,11 @@ file://$PROJECT_DIR$/app/controllers/copies_controller.rb - 40 + 36 - diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb index fca8fbf..660be33 100755 --- a/app/controllers/copies_controller.rb +++ b/app/controllers/copies_controller.rb @@ -1,22 +1,25 @@ class CopiesController < ApplicationController before_action :find_book - def find_book; end + def find_book + if params[:book_id].blank? + @copy = Copy.find(params[:id]) + @book = Book.find(@copy.book_id) + else + @book = Book.find(params[:book_id]) + end + end def index - @book = Book.find(params[:book_id]) @copies = @book.copies end def new - @book = Book.find(params[:book_id]) @copy = @book.copies.new end def create - @book = Book.find(params[:book_id]) @copy = @book.copies.new(copy_params) - if @copy.save redirect_to action: 'index' else @@ -29,16 +32,12 @@ def copy_params params.require(:copy).permit(:borrower, :due_date) end - def edit - @copy = Copy.find(params[:id]) - end + def edit; end def update - @copy = Copy.find(params[:id]) - if @copy.update_attributes(copy_params) - redirect_to book_copies_path(@copy.book_id) + redirect_to book_copies_path(@book) else render action: 'edit' end @@ -46,6 +45,6 @@ def update def destroy Copy.find(params[:id]).destroy - redirect_to action: 'index' + redirect_to action: book_copies_path(@book) end end diff --git a/app/views/books/_book_form.html.erb b/app/views/books/_book_form.html.erb index 5a9d610..bfa066d 100755 --- a/app/views/books/_book_form.html.erb +++ b/app/views/books/_book_form.html.erb @@ -9,5 +9,5 @@
- <%= link_to 'Return', @book.id ? book_path(@book) : book_path %> + <%= link_to 'Return', @book.id ? book_path(@book) : books_path %> <% end -%> \ No newline at end of file diff --git a/app/views/copies/_copy_form.html.erb b/app/views/copies/_copy_form.html.erb index 8979542..de56061 100755 --- a/app/views/copies/_copy_form.html.erb +++ b/app/views/copies/_copy_form.html.erb @@ -1,4 +1,5 @@ -<%= form_with model: [@book, @copy] do |f| %> + +<%= form_with model: @copy do |f| %>

<%= f.label :borrower %>: <%= f.text_field :borrower %>