-> 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):
-> 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
--> 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).
Create Reservations_
--> first let's get a reservation model going:
-> 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):
-> run it:
--> go into your reservation.rb model
-> make the variables accessible:
-> now we need to make sure that email and requested time are always required fields. Add the following to your reservations.rb model
-> 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:
Add to your restaurant.rb model:
--> test the relationship in your rails console (the foreign_key/restaurant_id should now be appended to the 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:
Get the view and controller to work:
Start with the controller:
--> add the create action to the reservations_controller.rb
--> now we also know that we have to destroy the reservation. Add this to the controller as well:
--> 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).
--> create the form partial in the reservations folder like this (I added the simple_form gem in my gemfile to keep forms simple):
--> 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:
-> 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:
--> add this above the link to the partial:
--> test things.