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>
check_avalability.html.erbin multiple views, include it in a partial. Create a viewviews/bookings/new.html.erb, create a route path inroute.rbget '/bookings/new', => 'bookings#new'or useresources :bookings. Inside the view new.html.erb you canrenderthe partialform_tagroute in mycheck_availability.html.erbfile 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.GETrequest instead of aPUTrequest. Actually I understand this now and Thanks for explaining it to me. I totally think you should give yourself an answer, it can be useful for many of us and If you have additional problem write it down in this discussion as I may have similar too!<%= form_tag user_booking_path(current_user.id, :id), multipart: true do %>returns/users/2/bookings/id. Any idea how i should resolve this? It should be/users/:user_id/bookings/:id.