Pass date by Ruby on Rails parameter

Asked

Viewed 507 times

1

I’m trying to pass a date clicked as a parameter in order to load another view and I can’t. The idea is to list all bookings made on the selected day.

Controller
class ReservasController < ApplicationController
 ...

  def search
    @date = Date.new(params[:data]).to_s
    @reservas = 
    Reserva.search_by_day(@date.strftime("%d/%m/%Y"),current_user)
  end

Routes
Rails.application.routes.draw do

  get 'usuarios/new'
  get    'sign_in'   => 'sessions#new'
  post   'sign_in'   => 'sessions#create'
  delete 'sign_out'  => 'sessions#destroy'
  get 'reservas/search' => 'reservas#search'

View Index
<h1>Reservas</h1>
<div id="calendar">
  <h2 id="month">
        <%= link_to "<", month: (@date.beginning_of_month-1).strftime("%b%Y") %>
        <%=h (@date.strftime("%B / %Y")) %>
        <%= link_to ">", month: (@date.end_of_month+1).strftime("%b%Y") %>
    </h2>
    <%= calendar_for @reservas, year: @date.year, month: @date.month do |t| %>
     <%= t.head('DOM','SEG', 'TER', 'QUA', 'QUI', 'SEX', 'SAB') %>
     <%= t.day(day_method: :data) do |day, reservas| %>
      <%= link_to day.strftime('%d-%m/%Y'),reservas_search_path(day.strftime('%d-%m/%Y')) %><br />

Model Reserva
def self.search_by_day(dia, current_user)
    if current_user.perfil_id == 3
      Reserva.where("strftime('%d/%m/%Y', data) = ? AND
                    realizado = ? AND profissional_id = ?", dia,
                    false, current_user.profissional_id).order('data, hora ASC')
    else
      Reserva.where("strftime('%d/%m/%Y', data) = ? AND
                    realizado = ?", dia, false).order('data, hora ASC')
    end
  end

1 answer

0


Simply assign the value to the parameter that will be used in the controller. <%= link_to day.strftime('%d/%m/%Y'),reservas_search_path(:data => day.strftime('%d-%m/%Y')) %>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.