1

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>

6
  • if you use check_avalability.html.erb in multiple views, include it in a partial. Create a view views/bookings/new.html.erb, create a route path in route.rb get '/bookings/new', => 'bookings#new' or use resources :bookings. Inside the view new.html.erb you can render the partial Commented Mar 11, 2017 at 11:03
  • 1
    Thanks @FabrizioBertoglio! It was a routing issue. 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. Commented Mar 11, 2017 at 17:28
  • Yes, right. That was the real problem. Your form should do a GET request instead of a PUT request. 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! Commented Mar 11, 2017 at 17:59
  • @FabrizioBertoglio I am still very new to coding and stackoverflow! I just posted an answer. I am actually still struggling to understand routing especially in a nested resources. So now i am trying to save my form in "new" and show the booking details on "show". <%= 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. Commented Mar 11, 2017 at 18:27
  • I am not sure of the problem you are having. Usually this are the step that you should have: 1. GET request for "/booking/new" => "bookings#new" 2. POST request "booking" => "bookings#create" With the post request you save to the database your details 3. GET request "booking/:id" => "bookings#show" Hear you have the show request I think with nested resource it is just the same. The best thing you can do is rewrite your question with clear explanation of the issue. Commented Mar 11, 2017 at 18:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.