rails refreshing date in datetime_select option

1k Views Asked by At

First of all I'm new to the HTML and RAILS coding. But as a part of learning experience, I've designed a machine reservation system for the lab I'm working in. I've a form (show.html.erb) to view a machine details like name, ipaddress etc. below that there is a reservation form with datetime_select, number of hours option as shown below,

As you can see it's really basic html coding. ... Machine details Name: <%= @machine.name %>
Ipaddress: <%= @machine.ipaddress %>
Processor: <%= @machine.processor %>
Memory: <%= @machine.memory %>
Operating System: <%= @machine.os %>
Description: <%= @machine.description %>
Machine status: <% if @machine.active == true %> Online <% else %> Offline <% end %>
<% if @machine.can_reserved == false %> This machine can not be reserved. See description <% end %>

   <fieldset>
    <legend>Create a New Reservation</legend>
      <p style="color: red"><%= flash[:error] %></p>
      <%= form_for [@machine, @reservation] do |f| %>
          <div class="field"> 
              <%= f.label :startdate, 'Start Time' %>:
              <%= f.datetime_select :startdate %> (<b>PST</b> timezone)
          </div>
          <div class="field">
              <%= f.label :purpose, 'Reason' %>:
              <%= f.text_field :purpose, :size => 40 %>
          </div>
          <div class="field">
              <%= f.label :hour, 'Hour(s)' %>:
              <%= f.text_field :hour, :size => 2 %>
          </div>
          <div class="field">
              <%= f.label :days, 'No of Days' %>:
              <%= f.text_field :days, :size => 2 %>
          </div>
          <div class="actions">
              <%= f.submit 'Reserve this machine' %>
          </div>
      <% end %>
      <br />
      </fieldset>

... Now when you clicks show action for a machine, it does everything right. Shows all Machine details, and show current time in the reservation form. But as I refresh the page after let's say a minute, it refreshes machine details info but it doesn't (refresh) change the time already shown with the datetime_select option. Why? How can it always show current time? I understand the views are nothing but a static pages. I added tag to the page which again refreshes the whole page automatically but doesn't seem to affect date shown in the datetime_select option.

Any thoughts? I really appreciate the help.

Thanks.

Tom

1

There are 1 best solutions below

1
On

There are a couple of ways to do this, I am going to show you one way:

routes.rb:

resources :machines do
  resources :reservations
end

This will yeild paths like /machines/1/reservations and /machines/1/reservations/new etc. Run rake routes to view the generated routes and their helper method names.

reservation.rb

class Reservation < ActiveRecord::Base
  belongs_to :machine
end

machine.rb

class Machine < ActiveRecord::Base
  has_many :reservations, :dependent => :destroy
end

reservations_controller.rb

Update the new method as follows:

def new
  @machine = Machine.find params[:machine_id]
  @reservation = Reservation.new(:machine => @machine)
end

views/reservations/_form.html.erb

<%= form_for [:machine, @reservation] do |f| %>
    ... put your machine data stuff here ...
    ... put your form fields here ...
<% end %>

That should do the trick. Keep in mind that with the routes setup as I outlined you will need to update the route helpers found in the code in various places. Use rake routes to see what those route names are etc.

Also, create the link to create a new reservation (probably on the show machine page?) using something like this:

link_to "New Reservation", new_machine_reservation_path(@machine)

Of course, substitute @machine if needs be with whatever variable is storing the machine you want to make a reservation for.

Also, don't forget to update reservations_controller#create as well!

I hope this helps.