Rails,在呈现某些内容后更新数据库记录?

时间:2010-09-20 19:46:40

标签: ruby-on-rails

有没有办法在渲染某些内容后更新记录而不将代码放在我的视图中?

例如,我向用户显示消息,在显示消息后,我想将read属性更新为true。这样做的最佳做法是什么?

1 个答案:

答案 0 :(得分:2)

在rails 2.3中,您可以使用与after_filter相同的方式执行before_filter。所以,例如:

class MyController << ApplicationController
  after_filter :mark_read

  def mark_read
    @message.update_attribute(:read, true)
  end
end