Skip to content

Live code #4

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 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions app/controllers/house_plants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@ class HousePlantsController < ApplicationController
def index
@house_plants = HousePlant.all
end

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

def new
@house_plant = HousePlant.new
end

def create
@house_plant = HousePlant.new(house_plant_params)

if @house_plant.save
redirect_to house_plant
else
render :new
end
end

private

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

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

validates :height, numericality: { greater_than_or_equal_to: 1 }
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>

<p><%= link_to "Create Plant", new_house_plant_path %></p>

<ol>
<% @house_plants.each do |plant| %>
<li><%= plant.plant_type %></li>
<li><%= link_to plant.plant_type, house_plant_path(plant) %></li>
<% end %>
</ol>
28 changes: 28 additions & 0 deletions app/views/house_plants/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% if @house_plant.errors.any? %>
<div>
<h2>
<%= pluralize(@house_plant.errors.count, "error") %>
prohibited this plant from being saved:
</h2>

<ul>
<% @house_plant.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<h1>New Plant</h1>

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

<%= form.label :height %>
<%= form.text_field :height %>

<%= form.collection_select :room_id, Room.all, :id, :name %>

<%= form.submit "Create Plant" %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/house_plants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= @house_plant.plant_type %></h1>
<p><%= @house_plant.height %> inches</p>
<p><%= link_to @room.name, room_path(@room) %></p>
2 changes: 1 addition & 1 deletion app/views/rooms/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<ol>
<% @rooms.each do |room| %>
<li><%= room.name %></li>
<li><%= link_to room.name, room_path(room) %></li>
<% end %>
</ol>
9 changes: 9 additions & 0 deletions app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1><%= @room.name %></h1>
<p>Occupancy: <%= @room.occupancy %> people</p>

<h2>House Plants</h2>
<ul>
<% @house_plants.each do |plant| %>
<li><%= link_to plant.plant_type, plant %></li>
<% end %>
</ul>
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: %i[index show]
resources :house_plants, except: %i[edit update destroy]
end
5 changes: 5 additions & 0 deletions db/migrate/20240822180119_add_room_id_to_house_plants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRoomIdToHousePlants < ActiveRecord::Migration[5.2]
def change
add_reference :house_plants, :room
end
end
27 changes: 27 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_08_22_180119) do

create_table "house_plants", force: :cascade do |t|
t.string "plant_type"
t.integer "height"
t.integer "room_id"
t.index ["room_id"], name: "index_house_plants_on_room_id"
end

create_table "rooms", force: :cascade do |t|
t.string "name"
t.integer "occupancy"
end

end