Louis | Adding mark-as-sold button to bike edit page

This commit is contained in:
Loos
2014-09-20 12:00:17 -05:00
parent 1b6255a20c
commit b8fc1eff42
6 changed files with 41 additions and 19 deletions
+15
View File
@@ -4,6 +4,10 @@ class BikesController < ApplicationController
def index
@bikes = Bike.all
@unsold_bikes = @bikes.select{|bike|
!bike.date_sold &&
(bike.purpose == "Sale")
}
end
def show
@@ -47,6 +51,17 @@ class BikesController < ApplicationController
redirect_to bikes_url
end
def mark_as_sold
current_date = Time.new.strftime("%Y-%m-%d")
@bike = Bike.find(params[:id])
if @bike.update_attribute(:date_sold, current_date)
redirect_to bikes_url, notice: @bike.name + ' was marked as sold on ' + current_date + '.'
else
render action: edit, notice: 'Bike sale date could not be updated'
end
end
private
def set_bike
@bike = Bike.find(params[:id])
+5 -1
View File
@@ -1,5 +1,9 @@
.container
%h1 Editing bike
.row
.col-sm-6
%h1 Edit bike
.col-sm-3.col-sm-offset-3
=button_to "Mark as sold", {action: "mark_as_sold", id: @bike.id}, method: :patch, class: "btn btn-default"
= render 'form'
= link_to 'Show', @bike
|
+19 -4
View File
@@ -1,6 +1,9 @@
.container
%h1 Bikes
= link_to ' + Log Bike', new_bike_path, class: "btn btn-default"
%br
%h1 Unsold Bikes
%table.table.table-striped.table-bordered.table-hover
%thead
%tr
@@ -8,6 +11,21 @@
%th Model
%th
%th
%tbody
- @unsold_bikes.each do |bike|
%tr
%td= bike.brand
%td= bike.model
%td= link_to 'Show', bike
%td= link_to 'Edit', edit_bike_path(bike)
%h1 All Bikes
%table.table.table-striped.table-bordered.table-hover
%thead
%tr
%th Brand
%th Model
%th
%th
%tbody
- @bikes.each do |bike|
@@ -16,7 +34,4 @@
%td= bike.model
%td= link_to 'Show', bike
%td= link_to 'Edit', edit_bike_path(bike)
%td= link_to 'Destroy', bike, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to ' + Log Bike', new_bike_path, class: "btn btn-default"
+1 -1
View File
@@ -21,4 +21,4 @@
.strong New Parts:
=bike.new_parts
.row
.col-xs-6.col-xs-offset-6.small= "Log Number: " + bike.log_number.to_s
.col-xs-9.col-xs-offset-3.small= "Log Number: " + bike.log_number.to_s