0

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

  • can you also provide the code for the controller method that loads this form? – brettish Apr 19 '11 at 18:08
  • @brettish, here is that code from machines_controller.rb file, def show @machine = Machine.find(params[:id]) @reservation = Reservation.new @reservation.user_id = session[:user_id] @noOfDays = 7 respond_to do |format| format.html # show.html.erb format.xml { render :xml => @machine } end end – user715698 Apr 19 '11 at 18:13
  • try adding `@reservation.startdate = DateTime.now` and see if that does anything. – brettish Apr 19 '11 at 18:18
  • Also another thing to mention is, when user clicks on the 'Reserve this machine' action, it goes and creates the reservation, and comes back to the same page, but this time, 'datetime_select' shows the current time. – user715698 Apr 19 '11 at 18:19
  • @brettish, @reservation.startdate = DateTime.now didn't do the trick. – user715698 Apr 19 '11 at 18:24
  • Is it possible that it's using the cached values for the date time? – user715698 Apr 19 '11 at 18:30

1 Answers1

0

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.

brettish
  • 2,628
  • 3
  • 17
  • 22
  • I've the routes, models set as you suggested except the reservation controller. It has only two methods 'create' and 'destroy'. May be it has not setup completely the RAILS way. I'll try your suggestion, all of it and let you know. Thanks for your time. I really appreciate it. - Tom – user715698 Apr 19 '11 at 19:08