我希望能够在我的应用中混淆对象的ID,以便可以共享URL,但不容易猜到。
我已经收集到我需要在对象的数据库中存储hashed_id
,并且可以重写to_param
函数以使用hashed_id
中的 after_create :create_hashed_id
def to_param
self.hashed_id
end
def create_hashed_id
self.update_attributes(:hashed_id => Digest::SHA1.hexdigest([Time.now, rand].join)[0,16])
end
网址,但我无法让它工作。
模型:
def show
@upload = Upload.find_by_hashed_id(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @upload }
end
end
控制器:
id
使用hash_id属性创建对象,但是当我尝试调用show方法时,它看起来像是在hashed_id
列而不是{{1}}列。