Skip to content

Create Plant App #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ git_source(:github) do |repo_name|
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
gem 'rails', '~> 5.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '1.5'
# Use Puma as the app server
Expand All @@ -31,6 +31,8 @@ gem 'jbuilder', '~> 2.5'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

gem 'bootsnap', require: false

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand Down
113 changes: 63 additions & 50 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.1.7)
actionpack (= 5.1.7)
actioncable (5.2.8.1)
actionpack (= 5.2.8.1)
nio4r (~> 2.0)
websocket-driver (~> 0.6.1)
actionmailer (5.1.7)
actionpack (= 5.1.7)
actionview (= 5.1.7)
activejob (= 5.1.7)
websocket-driver (>= 0.6.1)
actionmailer (5.2.8.1)
actionpack (= 5.2.8.1)
actionview (= 5.2.8.1)
activejob (= 5.2.8.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.1.7)
actionview (= 5.1.7)
activesupport (= 5.1.7)
rack (~> 2.0)
actionpack (5.2.8.1)
actionview (= 5.2.8.1)
activesupport (= 5.2.8.1)
rack (~> 2.0, >= 2.0.8)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.1.7)
activesupport (= 5.1.7)
actionview (5.2.8.1)
activesupport (= 5.2.8.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.1.7)
activesupport (= 5.1.7)
activejob (5.2.8.1)
activesupport (= 5.2.8.1)
globalid (>= 0.3.6)
activemodel (5.1.7)
activesupport (= 5.1.7)
activerecord (5.1.7)
activemodel (= 5.1.7)
activesupport (= 5.1.7)
arel (~> 8.0)
activesupport (5.1.7)
activemodel (5.2.8.1)
activesupport (= 5.2.8.1)
activerecord (5.2.8.1)
activemodel (= 5.2.8.1)
activesupport (= 5.2.8.1)
arel (>= 9.0)
activestorage (5.2.8.1)
actionpack (= 5.2.8.1)
activerecord (= 5.2.8.1)
marcel (~> 1.0.0)
activesupport (5.2.8.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (8.0.0)
arel (9.0.0)
base64 (0.2.0)
bcrypt (3.1.20)
bindex (0.8.1)
bootsnap (1.18.4)
msgpack (~> 1.2)
builder (3.3.0)
byebug (11.1.3)
coffee-rails (4.2.2)
Expand All @@ -56,7 +62,7 @@ GEM
date (3.3.4)
erubi (1.13.0)
execjs (2.9.1)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.14.5)
Expand All @@ -79,9 +85,12 @@ GEM
net-imap
net-pop
net-smtp
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
minitest (5.24.1)
mini_portile2 (2.8.7)
minitest (5.25.1)
msgpack (1.7.2)
net-imap (0.4.14)
date
net-protocol
Expand All @@ -92,25 +101,27 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.15.6-arm64-darwin)
nokogiri (1.15.6)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
puma (6.4.2)
nio4r (~> 2.0)
racc (1.8.1)
rack (2.2.9)
rack-test (2.1.0)
rack (>= 1.3)
rails (5.1.7)
actioncable (= 5.1.7)
actionmailer (= 5.1.7)
actionpack (= 5.1.7)
actionview (= 5.1.7)
activejob (= 5.1.7)
activemodel (= 5.1.7)
activerecord (= 5.1.7)
activesupport (= 5.1.7)
rails (5.2.8.1)
actioncable (= 5.2.8.1)
actionmailer (= 5.2.8.1)
actionpack (= 5.2.8.1)
actionview (= 5.2.8.1)
activejob (= 5.2.8.1)
activemodel (= 5.2.8.1)
activerecord (= 5.2.8.1)
activestorage (= 5.2.8.1)
activesupport (= 5.2.8.1)
bundler (>= 1.3.0)
railties (= 5.1.7)
railties (= 5.2.8.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
Expand All @@ -119,12 +130,12 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (5.1.7)
actionpack (= 5.1.7)
activesupport (= 5.1.7)
railties (5.2.8.1)
actionpack (= 5.2.8.1)
activesupport (= 5.2.8.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
thor (>= 0.19.0, < 2.0)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
Expand All @@ -134,8 +145,8 @@ GEM
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
sass-rails (5.0.7)
railties (>= 4.0.0, < 6)
sass-rails (5.1.0)
railties (>= 5.2.0)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
Expand All @@ -148,11 +159,12 @@ GEM
base64
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.5.0-arm64-darwin)
sqlite3 (1.5.0)
mini_portile2 (~> 2.8.0)
thor (1.3.1)
thread_safe (0.3.6)
tilt (2.4.0)
Expand All @@ -169,22 +181,23 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
websocket-driver (0.6.5)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
byebug
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
puma
rails (~> 5.1.6)
rails (~> 5.2)
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
Expand All @@ -195,4 +208,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
2.4.22
2.1.4
24 changes: 24 additions & 0 deletions app/controllers/house_plants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@ class HousePlantsController < ApplicationController
def index
@house_plants = HousePlant.all
end

def show
@house_plant = HousePlant.find(params[:id])
end

def new
@house_plant = HousePlant.new
@rooms = Room.all
@house_plant.room_id = params[:room_id] if params[:room_id]
end

def create
@house_plant = HousePlant.new(house_plant_params)
@rooms = Room.all
if @house_plant.save
redirect_to house_plant_path(@house_plant)
else
render :new
end
end

def house_plant_params
params.require(:house_plant).permit(:plant_type, :height, :room_id)
end
end
4 changes: 4 additions & 0 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class RoomsController < ApplicationController
def index
@rooms = Room.all
end

def show
@room = Room.find(params[:id])
end
end
7 changes: 7 additions & 0 deletions app/models/house_plant.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class HousePlant < ApplicationRecord
belongs_to :room

validates :plant_type, :height, :room, presence: true
validates :height, numericality: { greater_than_or_equal_to: 1 }

def room_name
room.name
end
end
2 changes: 1 addition & 1 deletion app/models/room.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Room < ApplicationRecord

has_many :house_plants
end
4 changes: 3 additions & 1 deletion app/views/house_plants/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h1>House Plants</h1>

<h2><%= link_to 'Add New Plant', new_house_plant_path %></h2>

<ol>
<% @house_plants.each do |plant| %>
<li><%= plant.plant_type %></li>
<li><%= link_to plant.plant_type, house_plant_path(plant) %></li>
<% end %>
</ol>
36 changes: 36 additions & 0 deletions app/views/house_plants/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h1>Add New Plant:</h1>

<% if @house_plant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@house_plant.errors.count, "error") %> prohibited this house plant from being saved:</h2>
<ul>
<% @house_plant.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_with model: @house_plant, local: true do |form| %>
<div>
<%= form.label :plant_type %>
<%= form.text_field :plant_type %>
</div>

<div>
<%= form.label :height %>
<%= form.number_field :height %>
</div>

<div>
<%= form.label :room_id %>
<%= form.collection_select :room_id, @rooms, :id, :name, prompt: "Select a room" %>
</div>

<div>
<%= form.submit "Create Plant" %>
</div>
<% end %>

<h4><%= link_to 'Back to Plants', house_plants_path %></h4>
<h4><%= link_to 'Back to Rooms', rooms_path %></h4>
7 changes: 7 additions & 0 deletions app/views/house_plants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%= @house_plant.plant_type %></h1>

<p><%= @house_plant.height %> inches</p>

<h2><%= link_to @house_plant.room_name, room_path(@house_plant.room) %></h2>

<h4><%= link_to 'Back to Plants', house_plants_path %></h4>
6 changes: 5 additions & 1 deletion app/views/rooms/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<ol>
<% @rooms.each do |room| %>
<li><%= room.name %></li>
<li><%= link_to room.name, room_path(room) %></li>
<% end %>
</ol>



<h2><%= link_to 'Create New Plant', new_house_plant_path %></h2>
14 changes: 14 additions & 0 deletions app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1><%= @room.name %></h1>

<h3>Occupancy:</h3>
<p><%= @room.occupancy %> people</p>

<h3>Plants:</h3>
<ul>
<% @room.house_plants.each do |plant| %>
<li><%= link_to plant.plant_type, house_plant_path(plant) %></li>
<% end %>
</ul>

<h2><%= link_to 'Add New Plant to this Room', new_house_plant_path(room_id: @room.id) %></h2>
<h4><%= link_to 'Back to Rooms', rooms_path %></h4>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rails.application.routes.draw do
resources :rooms, only: [:index]
resources :house_plants, only: [:index]
resources :rooms, only: [:index, :show]
resources :house_plants, only: [:index, :show, :new, :create]
end
1 change: 1 addition & 0 deletions db/migrate/20190610081818_create_house_plants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def change
create_table :house_plants do |t|
t.string :plant_type
t.integer :height
t.references :room
end
end
end
Loading