简体   繁体   English

如何在Rails中仅使10分钟的“编辑发布”方法可用?

[英]How to make “edit post” method available for only 10 minutes in Rails?

I am making a Instagram clone as a mini project to understand how to use Rails, which I haven't had any prior experience with. 我正在将Instagram克隆作为一个迷你项目,以了解如何使用Rails,我之前没有任何经验。 One of the "client" requirements is to be able to edit their comments on a post but only for up to 15 minutes. “客户”要求之一是能够编辑他们对帖子的评论,但最多只能持续15分钟。

I have done some research but am struggling to find the best way of implementing this feature. 我已经做过一些研究,但是正在努力寻找实现此功能的最佳方法。 Is there something in Rails that would help me to do this? Rails中有什么可以帮助我做到这一点的吗?

Thanks :) 谢谢 :)

I would use a method to check if it's editable such as: 我将使用一种方法来检查其是否可编辑,例如:

def editable?
  Time.now - self.created_at < 10.minutes
end

It needs validation as well: 它也需要验证:

def validate_is_editable
  if self.persisted? && !self.editable?
    self.errors[:editable] << "can edit in just 10 minutes after creation"
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只允许帖子的作者编辑或在Ruby on Rails中删除它? - How to allow only the author of a post edit or delete it in Ruby on Rails? 如何在Rails 3.2的视图和控制器中使用下划线方法? - How to make underscore method available in view and controller in rails 3.2? 如何在Rails中为多个模型提供相同的方法? - How can I make the same method available to multiple models in Rails? Rails:如何在setter方法中提供父属性 - Rails: How to make available parent attribute in setter method 如何让工厂可以使用辅助方法? (导轨,Rspec) - How do I make a helper method available to factories? (Rails, Rspec) Rails 4:当前用户只能编辑他们的帖子 - Rails 4 : Current user can only edit their post 如何使我的ActiveRecord对象在Ruby on Rails中仅存在5分钟? - How can I make my ActiveRecord objects only exist for 5 minutes in Ruby on Rails? Rails 4 after_initialize仅在Edit方法上 - Rails 4 after_initialize Only on Edit method Ruby gem方法只在控制器中可用? 如果是这样,如何在其他文件中使用它? - Ruby gem method only available in controller? If so, how to make it available in other files? 如何在Rails中每隔几秒钟运行一些动作10分钟? - How to run some action every few seconds for 10 minutes in rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM