Permalink
Browse files

class complete

  • Loading branch information...
1 parent 88dc831 commit f3da862927dcd7f393ce16b2dda64d300d0697e5 @kornypoet committed Aug 9, 2012
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the hotels controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,54 @@
+class HotelsController < ApplicationController
+
+ before_filter :load_hotel, except: [:new, :index, :create]
+
+ def index
+ @hotels = Hotel.all
+ end
+
+ def show
+ respond_to do |format|
+ format.html
+ format.xml{ render xml: @hotel.to_xml }
+ format.json{ render json: @hotel.to_json }
+ end
+ end
+
+ def new
+ @cities = City.order(:name).all
+ @hotel = Hotel.new
+ end
+
+ def create
+ @hotel = Hotel.new(params[:hotel])
+ if @hotel.save
+ redirect_to @hotel
+ else
+ flash[:error] = @hotel.errors
+ @cities = City.order(:name).all
+ render :new
+ end
+ end
+
+ def edit
+ @cities = City.order(:name)
+ end
+
+ def update
+ if @hotel.update_attributes(params[:hotel])
+ redirect_to @hotel
+ else
+ flash[:error] = @hotel.errors
+ @cities = City.order(:name).all
+ render :new
+ end
+
+ end
+
+private
+
+ def load_hotel
+ @hotel = Hotel.find(params[:id])
+ end
+
+end
@@ -0,0 +1,7 @@
+module HotelsHelper
+
+ def day_today
+ Date.today.strftime "%A"
+ end
+
+end
View
@@ -0,0 +1,7 @@
+class City < ActiveRecord::Base
+
+ attr_accessible :name
+
+ has_many :hotels
+
+end
View
@@ -0,0 +1,22 @@
+class Hotel < ActiveRecord::Base
+
+ belongs_to :city
+
+ attr_accessible :name, :description, :city_id
+ validates :name, presence: true, length: { minimum: 2 }
+
+ before_save :ensure_capitalization
+
+ def ensure_capitalization
+ self.name = self.name.titlecase
+ end
+
+ class << self
+
+ def yet_to_visit
+ where(visited: false)
+ end
+
+ end
+
+end
View
@@ -0,0 +1,3 @@
+class Trip < ActiveModel::Base
+
+end
@@ -0,0 +1,12 @@
+<%= form_for @hotel do |f| %>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+ <%= f.label :description %>
+ <%= f.text_area :description %>
+
+ <%= f.label :city %>
+ <%= f.collection_select :city_id, @cities, :id, :name %>
+
+ <%= f.submit :save %>
+<% end %>
@@ -0,0 +1,3 @@
+<h1>Edit Hotel<h1>
+
+<%= render partial: 'form' %>
@@ -0,0 +1,8 @@
+<h1>Hotels as of <%= day_today %></h1>
+
+<p>Here are our hotels:</p>
+<ul>
+ <% @hotels.each do |hotel| %>
+ <li><%= link_to hotel.name, hotel %></li>
+ <% end %>
+</ul>
@@ -0,0 +1,3 @@
+<h1>Create Hotel</h1>
+
+<%= render partial: 'form' %>
@@ -0,0 +1,7 @@
+<h1><%= @hotel.name %></h1>
+<div>
+ <u1><%= @hotel.description %></u1>
+ </div>
+<div>
+ <u1><%= @hotel.city.name %></u1>
+</div>
@@ -8,7 +8,11 @@
</head>
<body>
-<%= yield %>
+ <% if flash[:error] %>
+ <p><%= flash[:error] %></p>
+ <% end %>
+
+ <%= yield %>
</body>
</html>
View
@@ -1,4 +1,9 @@
LsrcRails::Application.routes.draw do
+
+ resources :hotels
+
+ root to: "hotels#index"
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,13 @@
+class CreateHotels < ActiveRecord::Migration
+ def up
+ create_table :hotels do |t|
+ t.string :name
+ t.text :description
+ t.timestamps
+ end
+ end
+
+ def down
+ drop_tabel :hotels
+ end
+end
@@ -0,0 +1,9 @@
+class CreateCities < ActiveRecord::Migration
+ def change
+ create_table :cities do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class AddCityReferenceToHotels < ActiveRecord::Migration
+ def change
+ add_column :hotels, :city_id, :integer
+ end
+end
@@ -0,0 +1,5 @@
+class AddVisitedToHotels < ActiveRecord::Migration
+ def change
+ add_column :hotels, :visited, :boolean
+ end
+end
View
@@ -0,0 +1,31 @@
+# encoding: UTF-8
+# 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 to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120809192701) do
+
+ create_table "cities", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "hotels", :force => true do |t|
+ t.string "name"
+ t.text "description"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "city_id"
+ t.boolean "visited"
+ end
+
+end
View
@@ -3,5 +3,7 @@
#
# Examples:
#
-# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
+#
# Mayor.create(name: 'Emanuel', city: cities.first)
+
+cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
Oops, something went wrong.

0 comments on commit f3da862

Please sign in to comment.