0

首先,我是 HTML 和 RAILS 编码的新手。但作为学习经验的一部分,我为我正在工作的实验室设计了一个机器预订系统。我有一个表单 (show.html.erb) 来查看机器详细信息,如名称、ipaddress 等。有一个带有 datetime_select,小时数选项的预订表格,如下所示,

如您所见,这是非常基本的 html 编码。... 机器详细信息 名称: <%= @machine.name %>
Ipaddress: <%= @machine.ipaddress %>
处理器: <%= @machine.processor %>
内存: <%= @machine.memory %> 正在
运行系统: <%= @machine.os %>
描述: <%= @machine.description %>
机器状态: <% if @machine.active == true %> 在线 <% else %> 离线 <% end %>
< % if @machine.can_reserved == false %> 这台机器不能被保留。 <% 结束 %>

   <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>

...现在,当您单击机器的显示操作时,它会正确执行所有操作。显示所有机器详细信息,并在预订表格中显示当前时间。但是当我在一分钟后刷新页面时,它会刷新机器详细信息,但它不会(刷新)更改已使用 datetime_select 选项显示的时间。为什么?它怎么总是显示当前时间?我理解视图只不过是一个静态页面。我向页面添加了标签,该标签再次自动刷新整个页面,但似乎不会影响 datetime_select 选项中显示的日期。

有什么想法吗?我真的很感激帮助。

谢谢。

汤姆

4

1 回答 1

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.

于 2011-04-19T18:56:18.740 回答