MarcoMorawec / SiB-Dev-Team

All SiB Dev Team Mojo
4 stars 0 forks source link

Lab 4 Shortcuts #18

Open MarcoMorawec opened 11 years ago

MarcoMorawec commented 11 years ago

Create Reservations_

--> first let's get a reservation model going:

rails generate model reservation 

-> in the migration file that was genrated with it let's add the variables we need to make this work (you might not need all of it, but that's what I have):

  def change
    create_table :reservations do |t|
      t.string :guestname
      t.string :guestemail
      t.integer :guestnumber
      t.string :message
      t.datetime :reserve_on
      t.time :reservationtime
      t.date :reservationdate
      t.integer :restaurant_id

      t.timestamps
    end

-> run it:

rake db:migrate

--> go into your reservation.rb model

-> make the variables accessible:

  attr_accessible :guestname, :guestemail, :guestnumber, :message, :reserve_on,
                  :reservationdate, :reservationtime, :restaurant_id

-> now we need to make sure that email and requested time are always required fields. Add the following to your reservations.rb model

  validates_presence_of  :guestemail, :reserve_on

-> now we know that we need to define the relationship between the restaurant and the reservations. It's a one-to-many relationship similar to the owner/restaurant relationship. One restaurant has-many reservations and one reservation belongs_to one restaurant.

Add to your reservation.rb model:

  belongs_to :restaurant

Add to your restaurant.rb model:

has_many :reservations

--> test the relationship in your rails console (the foreign_key/restaurant_id should now be appended to the reservation

reservation = Reservation.create(:guestemail => "me@gmail.com", :reserve_on => "2013-07-09 19:10:00")

Restaurant = Restaurant.find(1)

Restaurant.reservations << reservation

Say, fuck yeah and let's move on:

--> make sure your model knows that the reservation ressource is part of the restaurant ressource. Make sure you have this in your routes.rb:

  resource :restaurants
  resources :reservations

 # Sample resource route with sub-resources:
     resources :restaurants do
       resources :reservations
     end

Get the view and controller to work:

Start with the controller:

rails generate controller reservations

--> add the create action to the reservations_controller.rb

    def create
        @restaurant = Restaurant.find(params[:restaurant_id])
        @reservation = @restaurant.reservations.create(params[:reservation])    
            if @reservation.save
                flash[:notice] = "We saved your reservation and notified the restaurant!"
                redirect_to @restaurant
            else
                render ('restaurants/show')
            end
    end

--> now we also know that we have to destroy the reservation. Add this to the controller as well:

    def destroy
        @reservation = Reservation.find(params[:id])
        @reservation.destroy
        redirect_to :back , :notice => "Deleted reservation for #{@reservation.guestname}"
    end

--> Views are next. Open the show view for restaurants:

Add your make a reservation form here (I use a partial to store the form someplace else).

  <%= render :partial => '/reservations/reservation_form' %>

--> create the form partial in the reservations folder like this (I added the simple_form gem in my gemfile to keep forms simple):

<%= simple_form_for @reservation, :url => {:controller => 'reservations', :action => 'create', :restaurant_id => @restaurant.id}, :method => 'post', :html => {:class => 'form-horizontal' } do |f| %>
      <%= f.error_notification %>
      <%= f.input :guestname, label: 'Your Name'  %>
      <%= f.input :guestemail, label: 'Your Email' %>
      <%= f.input :guestnumber, label: 'How many people?' %>
      <%= f.input :reserve_on, label: 'When do you want to come?' %>                  
      <%= f.input :message, label: 'Anything else we need to know?' %>
      <%= f.button :submit, class: "btn btn-primary", value: "Create Reservation" %>
<% end %> 

--> Check your show view -> awesome we have a form!

Let's add the actual reservations table so owners can see the reservation for their restaurant:

                   <table class="table table-hover table-condensed">
                    <%= bootstrap_flash %>   
                    <thead>
                        <tr>
                        <strong>
                          <th>Name</th>
                          <th>Email</th>
                          <th>Guests</th>                           
                          <th>Reservation Time</th> 
                          <th>Message</th>           
                          <th><%=t '.actions', :default => t("helpers.actions") %></th>
                        </strong>
                        </tr>
                    </thead>
                    <tbody>            
                        <% @restaurant.reservations.each do |reservation| %> 
                        <tr>
                            <td><%= reservation.guestname %></td>
                            <td><%= reservation.guestemail %></td>
                            <td><%= reservation.guestnumber %></td>
                            <td><%= reservation.reserve_on %></td>       
                            <td><%= reservation.message %></td>
                            <td><%= link_to('Delete', reservation_path(reservation), :method => :delete, :confirm => "Press OK to confirm and delete this reservation?", :class => 'btn btn-danger') %>
                            </td>
                        </tr>
                        <% end %> 
                    </tbody>
                    </table>

-> make sure only owners see this table and regular users see the reservation form. Let's wrap the forms and table into an if/else statement.

--> add above the table:

        <% if current_owner == @restaurant.owner %>

--> add this above the link to the partial:

        <% else %>
                         <%= render :partial => '/reservations/reservation_form' %>
        <% end %>

--> test things.