-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to create cars and refuels
- Loading branch information
Showing
19 changed files
with
367 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* This file is a Sass file (http://sass-lang.com/) which is very | ||
similar to CSS3 but it is translated to CSS2 */ | ||
|
||
#content { | ||
width: 800px; | ||
margin: 0 auto; | ||
padding: 20px 10px; | ||
} | ||
|
||
table { | ||
margin: 20px 0; | ||
border-collapse: collapse; | ||
|
||
td, th { | ||
border: 1px solid #eee; | ||
padding: 6px; | ||
} | ||
|
||
thead tr { | ||
background-color: #ddd; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
form { | ||
margin: 0 0 25px 0; | ||
font-family: Helvetica; | ||
|
||
fieldset { | ||
padding: 10px; | ||
border: 1px solid #f1f1f1; | ||
} | ||
|
||
legend { | ||
padding: 0 8px; | ||
font-size: 1.1em; | ||
font-weight: bold; | ||
} | ||
|
||
label { | ||
margin: 0; | ||
padding: 0 0 3px 0; | ||
font-size: .8em; | ||
font-weight: bold; | ||
display: block; | ||
} | ||
|
||
input, textarea { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
input[type="text"], | ||
input[type="email"], | ||
input[type="url"], | ||
input[type="password"], | ||
textarea { | ||
width: 450px; | ||
height: 23px; | ||
font-size: 16px; | ||
border: 1px solid silver; | ||
} | ||
|
||
textarea { | ||
resize: none; | ||
font-size: 12px; | ||
height: 130px; | ||
} | ||
|
||
input[type="submit"], | ||
input[type="button"] { | ||
margin: 10px 0 10px 30px; | ||
padding: 4px; | ||
font-size: 1.2em; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,59 @@ | ||
class CarsController < ApplicationController | ||
|
||
############################################################################## | ||
# The following class method tells Rails what formats we can respond | ||
# with. This is necessary to use the `respond_with` helper later. | ||
respond_to(:html, :xml, :json) | ||
|
||
############################################################################## | ||
# Load all cars for the current user. Rails automatically renders | ||
# the app/views/cars/index.html.erb file. | ||
def index | ||
@cars = current_user.cars.order(:name) | ||
respond_with(@cars) | ||
end | ||
|
||
############################################################################## | ||
# Normally, if a model had enough information to display on a | ||
# dedicated page you would do so here in the show action. Since a | ||
# car only has a name it doesn't make much sense to show that by | ||
# itself, so we'll just redirect to the index action. | ||
def show | ||
redirect_to(cars_path) | ||
end | ||
|
||
############################################################################## | ||
# Using `respond_with` below will automatically render | ||
# app/views/cars/new.html.erb if the current request wants HTML, or | ||
# it can render XML or JSON if that's what the request calls for. | ||
def new | ||
@car = current_user.cars.new | ||
respond_with(@car) | ||
end | ||
|
||
############################################################################## | ||
def create | ||
@car = current_user.cars.create(params[:car]) | ||
respond_with(@car) | ||
end | ||
|
||
############################################################################## | ||
def edit | ||
@car = current_user.cars.find(params[:id]) | ||
respond_with(@car) | ||
end | ||
|
||
############################################################################## | ||
def update | ||
@car = current_user.cars.find(params[:id]) | ||
@car.update_attributes(params[:car]) | ||
respond_with(@car) | ||
end | ||
|
||
############################################################################## | ||
def destroy | ||
@car = current_user.cars.find(params[:id]) | ||
@car.destroy | ||
respond_with(@car) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class RefuelsController < ApplicationController | ||
|
||
############################################################################## | ||
# Establish a filter that will run before any of the actions in this | ||
# controller run. Its job will be to create a `@car` instance | ||
# variable that we can use to access refuel objects. The | ||
# `fetch_car` method can be found at the bottom of this file. | ||
before_filter(:fetch_car) | ||
|
||
############################################################################## | ||
# See the note in cars_controller.rb | ||
respond_to(:html, :xml, :json) | ||
|
||
############################################################################## | ||
def index | ||
@refuels = @car.refuels.order('refueled_at DESC') | ||
respond_with(@car, @refuels) # need @car because this is a nested resource | ||
end | ||
|
||
############################################################################## | ||
def show | ||
@refuel = @car.refuels.find(params[:id]) | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
def new | ||
@refuel = @car.refuels.new | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
def create | ||
@refuel = @car.refuels.create(params[:refuel]) | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
def edit | ||
@refuel = @car.refuels.find(params[:id]) | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
def update | ||
@refuel = @car.refuels.find(params[:id]) | ||
@refuel.update_attributes(params[:refuel]) | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
def destroy | ||
@refuel = @car.refuels.find(params[:id]) | ||
@refuel.destroy | ||
respond_with(@car, @refuel) | ||
end | ||
|
||
############################################################################## | ||
private | ||
|
||
############################################################################## | ||
# Since this controller is a nested resource under the cars | ||
# resource, all invocations will include a `:car_id` parameter to | ||
# tell us which car we are working with. | ||
def fetch_car | ||
@car = current_user.cars.find(params[:car_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module RefuelsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<%= form_for(@car) do |form| %> | ||
<fieldset> | ||
<legend><%= @title %></legend> | ||
|
||
<%# Error messages would go here %> | ||
|
||
<p> | ||
<%= form.label(:name) %> | ||
<%= form.text_field(:name) %> | ||
</p> | ||
|
||
<p> | ||
<%= button_to_function('Cancel', "window.location='#{cars_path}'") %> | ||
<%= form.submit %> | ||
</p> | ||
</fieldset> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<% @title = 'Edit Car' %> | ||
<%= render('form') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<% @title = 'New Car' %> | ||
<%# The following line renders app/views/cars/_form.html.erb %> | ||
<%= render('form') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Example</title> | ||
<%= stylesheet_link_tag "application" %> | ||
<%= javascript_include_tag "application" %> | ||
<%= csrf_meta_tags %> | ||
</head> | ||
<body> | ||
|
||
<%= yield %> | ||
|
||
</body> | ||
<head> | ||
<title><%= @title %></title> | ||
<%= stylesheet_link_tag "application" %> | ||
<%= javascript_include_tag "application" %> | ||
<%= csrf_meta_tags %> | ||
</head> | ||
<body> | ||
<div id="content"> | ||
<% if flash.notice %><div class="notice"><%= flash.notice %></div><% end %> | ||
<% if flash.alert %><div class="alert" ><%= flash.alert %></div><% end %> | ||
<%= yield %> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<%= form_for([@car, @refuel]) do |form| %> | ||
<fieldset> | ||
<legend><%= @title %></legend> | ||
|
||
<%# Error messages would go here %> | ||
|
||
<p> | ||
<%= form.label(:refueled_at, 'Refuel Date') %> | ||
<%= form.datetime_select(:refueled_at) %> | ||
</p> | ||
|
||
<p> | ||
<%= form.label(:odometer, 'Current Odometer Reading') %> | ||
<%= form.number_field(:odometer) %> | ||
</p> | ||
|
||
<p> | ||
<%= form.label(:gallons, 'Number of Gallons for This Refuel') %> | ||
<%= form.number_field(:gallons) %> | ||
</p> | ||
|
||
<p> | ||
<%= form.label(:price, 'Total Cost of This Refuel') %> | ||
<%= form.number_field(:price) %> | ||
</p> | ||
|
||
<p> | ||
<%= button_to_function('Cancel', "window.location='#{cars_path}'") %> | ||
<%= form.submit %> | ||
</p> | ||
</fieldset> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<% @title = 'Edit Refuel Record For Your ' + @car.name %> | ||
<%= render('form') %> |
Oops, something went wrong.