检查是否创建了新记录

时间:2015-09-09 06:31:18

标签: ruby-on-rails

我有一个User模型的表单accepts_nested_attributes has_manyComment的关联。

每当表单更新时,我都需要能够检查是否添加/创建了Comment

除了检查提交的params中的内容之外,还有更多的Rails方式吗?

用户模型

class User < ActiveRecord::Base
  accepts_nested_attributes_for :comments
  ...
end

查看

= simple_form_for @user do |f|
  = f.simple_fields_for :comments do |fc|
    = fc.input :content
    ...
  end
end

最后在我的控制器中

def update
  @user = User.find(params[:id])    
  if @user.update_attributes(permitted_params)
    # Check if a new comment was added here
  end
end

0 个答案:

没有答案
相关问题