Skip to content

Commit

Permalink
Intro to Ajaxifying book reservation:
Browse files Browse the repository at this point in the history
* Respond with JavaScript view templates
* Replace div element with reservation form using jQuery
* Show form with jQuery show()
  • Loading branch information
Tair Assimov committed Mar 2, 2012
1 parent 1e4b714 commit 26ad822
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/controllers/reservations_controller.rb
Expand Up @@ -3,6 +3,10 @@ class ReservationsController < ApplicationController
def new
@book = Book.find(params[:book_id])
@reservation = @book.reservations.new
respond_to do |format|
format.html
format.js
end
end

def create
Expand Down
8 changes: 6 additions & 2 deletions app/views/books/show.html.erb
Expand Up @@ -23,11 +23,15 @@
<% if @book_reservation %>
<%= link_to "Free", free_book_reservation_path(@book, @book_reservation), method: :put, class: 'btn' %>
<% else %>
<%= link_to "Reserve", new_book_reservation_path(@book), class: 'btn' %>
<%= link_to "Reserve", new_book_reservation_path(@book), class: 'btn', remote: true %>
<% end %>
<%= link_to "Delete", book_path, method: :delete, class: 'btn btn-danger' %>

<p>
<%= link_to "Back to list", books_path %>
</p>
</p>


<div id="reservation-form" style="display: none;">
</div>
21 changes: 21 additions & 0 deletions app/views/reservations/_new.html.erb
@@ -0,0 +1,21 @@
<h3>Reserve</h3>

<%= form_for [@book, @reservation] do |f| %>
<% unless @reservation.errors.empty? %>
<h2>Failed create reservation book</h2>
<% @reservation.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<% end %>

<div>
Email:<br/>
<%= f.text_field :email %>
</div>

<%= f.submit "Save" %>
or
<%= link_to "Cancel", book_path(@book) %>
<% end %>
1 change: 1 addition & 0 deletions app/views/reservations/new.js.erb
@@ -0,0 +1 @@
$('#reservation-form').html("<%=j render "new" %>").show();

0 comments on commit 26ad822

Please sign in to comment.