我正在創建一個博客網站,用戶可以發佈筆記,並且我想限制他們只能在發佈後的前3天內編輯帖子,並且遇到問題。如何限制用戶可以在Rails中編輯記錄的時間?
我有下面的代碼在我的筆記控制器
class NotesController < ApplicationController
before_filter :check_time!, only: [:edit, :update]
def edit
end
def create
end
private
def check_time!
if @note.created_at > @note.created_at + 3.hours
flash[:danger] = 'Out of 3 days'
redirect_to note_path(@note)
end
end
end
我有一個帖子是1日齡,以及用於測試目的,我在CHECK_TIME利用小時而不是幾天!方法在我的代碼中看看它是否工作,但它不。但是,由於某種原因,如果我從>更改爲<,它確實有效。
我敢肯定,我已經附上created_at時間戳使用以下遷移我的筆記:
class AddTimestampsToNote < ActiveRecord::Migration[5.0]
def change_table
add_column(:notes, :created_at, :datetime)
add_column(:notes, :updated_at, :datetime)
end
end
我真的不知道爲什麼,這並不工作,所以任何幫助,將不勝感激
嗨,謝謝!這固定它我相信 – Andrew