1
我是在Windows上使用rails和running rails 4的新手。我幾天來一直在處理這個錯誤。我認爲我輸入的以前的數據可能有問題,所以我嘗試在控制檯中創建一個新的預留,以查看發生了什麼問題,但目前爲止沒有運氣。預訂中的NoMethodError#index未定義的方法`strftime'爲零:NilClass
這是我的用戶模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :password, :remember_me
has_many :reservations
end
預訂模型:
class Reservation < ActiveRecord::Base
attr_accessible :user_id, :user_name, :start_time, :end_time, :project, :which_room
end
預訂指數:
<tbody>
<% @reservations.each do |reservation| %>
<tr>
<td><%= current_user.name %></td>
<td><%= reservation.project %></td>
<td><%= reservation.start_time.strftime("%I:%M %p")%></td>
<td><%= reservation.end_time.strftime("%I:%M %p")%></td>
<td><%= reservation.which_room %></td>
<td><%= link_to 'Edit', edit_reservation_path(reservation) %></td>
<td><%= link_to 'Destroy', reservation, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
顯示操作:
<p>
<strong>User:</strong>
<%= @user.name %>
</p>
<p>
<strong>Project:</strong>
<%= @reservation.project %>
</p>
<p>
<strong>Start time:</strong>
<%= @reservation.start_time %>
</p>
<p>
<strong>End time:</strong>
<%= @reservation.end_time %>
</p>
<p>
<strong>Room number:</strong>
<%= @reservation.which_room %>
</p>
和保留控制器: 類ReservationsController <的ApplicationController before_action:set_reservation,只有:[:顯示,編輯,:更新:摧毀]
def index
@reservations = Reservation.all.order(which_room: :asc, start_time: :desc)
end
def show
end
def new
@reservation = Reservation.new
end
def edit
end
def create
@reservation = Reservation.new(reservation_params)
respond_to do |format|
if @reservation.save
format.html { redirect_to @reservation, notice: 'Reservation was successfully created.' }
format.json { render action: 'show', status: :created, location: @reservation }
else
format.html { render action: 'new' }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @reservation.update(reservation_params)
format.html { redirect_to @reservation, notice: 'Reservation was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
def destroy
@reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url }
format.json { head :no_content }
end
end
private
def set_reservation
@reservation = Reservation.find(params[:id])
end
def reservation_params
params.require(:reservation).permit(:project, :start_time, :end_time, :which_room)
end
end
TIA!
對於特定條目?我應該從控制檯中刪除所有當前條目並刷新嗎? – eatsleepcode 2014-11-06 22:48:55
如果日期存在於您的視圖中,或者如果您想刪除所有,請在rails控制檯中執行Reservation.where('start_time IS NULL或end_time IS NULL'),只需添加一個「if」來檢查。destroy_all – 2014-11-06 23:23:55