Reputation: 23
I am working on a hotdesking app where the users would check_availability (check_availability.html.erb) of the desks based on the date range chosen and choose from a list of available desk (new.html.erb) before it gets saved in the database.
How can i use the form input in check_availability and use it under my 'new' method?
bookings_controller.rb
class BookingsController < ApplicationController
def index
end
def check_availability
end
def new
@booking = Booking.new(booking_params)
@booking.date_range(params[:book_from], params[:book_to])
end
def create
end
def show
end
private
def booking_params
params.permit(:book_from, :book_to, :wing, :section, :number, :user_id)
end
end
booking.rb
class Booking < ApplicationRecord
belongs_to :user, optional: true
belongs_to :desk, optional: true
accepts_nested_attributes_for :desk, allow_destroy: true
cattr_accessor :current_user
attr_accessor :book_from, :book_to
def check_availability
counter = 0
Booking.all.each do |b|
current_date = Date.today
booked_start_date = b.book_from
booked_end_date = b.book_to
if b.user_id == current_user.id && current_date <= booked_end_date && booked_start_date <= current_date
counter += 1
end
end
puts counter
end
def date_range(book_from, book_to)
a = []
a.push(book_from)
a.push(book_to)
puts a
end
routes.rb
Rails.application.routes.draw do
devise_scope :user do
authenticated :user do
root 'bookings#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
devise_for :users
resources :desks
post '/users/:user_id/bookings/:id', :to => 'bookings#show'
post '/users/:user_id/bookings/new', :to => 'bookings#new'
resources :users do
resources :bookings do
collection do
get :check_availability
end
end
end
end
check_availability.html.erb
<div class="container-fluid">
<div class="row">
<div class='col-sm-6 col-sm-offset-3'>
<h1>Check Availability</h1><hr />
</div>
<div class='col-sm-6 col-sm-offset-3'>
<%= form_tag new_user_booking_path, multipart: true do %>
<div class='form-group'>
<span class='not-bold'><%= label_tag :book_from, "Book From" %>: </span></br>
<span class='date-select'><%= date_select :book_from, class: 'form-control' %></span>
</div>
<div class='form-group'>
<span class='not-bold'><%= label_tag :book_to, "Book To" %>: </span><br />
<span class='date-select'><%= date_select :book_to, class: 'form-control' %></span>
</div>
<%= submit_tag "Check Availability", class: "btn" %>
<% end %>
</div>
<br />
<div class='col-sm-6 col-sm-offset-3'>
<br />
<hr />
<button type="button" class="button">
<%= link_to "My Bookings", authenticated_root_path %>
</button>
<button type="button" class="button">
<%= link_to "Book Desk", new_user_booking_path(current_user) %>
</button>
</div>
Upvotes: 1
Views: 135
Reputation: 23
It took me lots of tries to figure this out!
I realised it was a routing issue. The form should be doing a GET
instead of a PUT
request.
I changed the form_tag route in my check_availability.html.erb
file to <%= form_tag(new_user_booking_path, method: "get", class: "bookings") do %>
and i can now access the form inputs through the controller with params.
Upvotes: 1