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