我是铁杆新手。我敢肯定我的问题可能会用一两句话回答。我只是想弄清楚什么是错的。请帮忙!感谢您提前的时间。
所以这是我的问题:
我的控制器方法release_reservation
无法使用数据库中的新值保存属性。凭借我有限的铁路知识,我怀疑它与routes.rb
有关,但我无法缩小到实际问题。
这是[{1}}
release_reservation
reservations_controller.rb
这是模型def release_reservation
@reservation = Reservation.find(params[:id])
@reservation.reserve_from = DateTime.now
@reservation.reserve_to = DateTime.now
@reservation.reserve_status = false
respond_to do |format|
if @reservation.update_attributes(params[:reservation])
format.html {redirect_to release_reservation_reservation, :notice => 'Machine was successfully released' }# show.html.erb
format.json { render :json => @reservation }
else
format.html { render :action => "show" }
format.json { render :json => @reservation.errors, :status => :unprocessable_entity }
end
end
end
Reservation.rb
这是我想要呼叫class Reservation < ActiveRecord::Base
validates :machine_reserver, :presence => true, :length => { :maximum => 50 }
validates :machine_name, :uniqueness => {:scope => :machine_platform}
validate :check_reservation_end_date, :on => :update
validate :check_reservation_from_date
default_scope :order => 'updated_at DESC'
def date_reset
self.reserve_from = DateTime.now
self.reserve_to = DateTime.now
end
def check_reservation_end_date
if self.reserve_to < self.reserve_from
errors.add(:reserve_to, "cannot be lesser than or equal to start date")
end
end
def check_reservation_from_date
if self.reserve_from < DateTime.now
errors.add(:reserve_from, "cannot be lesser than current date & time")
end
end
end
release_reservation
这是<h1>Reservation</h1>
<br>
<p>
<% if @reservation.reserve_to > DateTime.now %>
This machine will be released in
<% duration = @reservation.reserve_to - DateTime.now %>
<%= distance_of_time_in_words(duration) %>
<% end %>
</p>
<p>
<b>Machine name: </b>
<%= @reservation.machine_name %>
</p>
<p>
<b>Machine platform: </b>
<%= @reservation.machine_platform %>
</p>
<p>
<% if @reservation.reserve_to > DateTime.now %>
<b>Reserved by: </b>
<%= @reservation.machine_reserver %>
<% end %>
</p>
<p>
<% if @reservation.reserve_to > DateTime.now %>
<b>Reserve from: </b>
<%= @reservation.reserve_from.to_formatted_s(:long) %>
<% end %>
</p>
<p>
<% if @reservation.reserve_to > DateTime.now %>
<b>Reserve to: </b>
<%= @reservation.reserve_to.to_formatted_s(:long) %>
<% end %>
</p>
<div class="form-actions">
<%= link_to 'Back', reservations_path, :class => 'btn' %>
<% if @reservation.reserve_to > DateTime.now %>
<%= link_to 'Release', {:controller => 'reservations', :action => 'release_reservation', :id => @reservation}, :confirm => "Are you sure?", :class => 'btn btn-danger' %>
<% else %>
<%= link_to 'Reserve', edit_reservation_path(@reservation), :class => 'btn btn-primary' %>
<% end %>
routes.rb
rake routes
Reserver::Application.routes.draw do
resources :reservations do
member do
get 'release_reservation'
end
end
resources :machines
resources :reserves
resources :platforms
resources :users
root :to => 'reservations#index'
end
答案 0 :(得分:0)
我认为您的验证会阻止实例持续