Rails控制器操作具有常见的样板 struct

  def update
    respond_to do |format|
      if @individual.update(individual_params)
        format.html { redirect_to @individual, notice: "Individual was successfully updated." }
       # if @individual.changed?  
        render_pdf
      # end
      else
        format.html { render :edit, status: :unprocessable_entity }
      end
    end
  end

然而,我们的目标是-在记录已更新的地方(除了updated_at属性)-激活将生成PDF的方法.

参考有关dirty的文档,我无法生成对changed?方法的真实响应.我怀疑这是因为调用方法的时间(if @individual.update表示记录已经保存,因此清除了污点).

上述内容应该如何写才能达到目标呢?

alternative
a callback after_update_commit might be an option, however, the rails guide is not explicit as to whether the action is launched when no attributes are updated.

推荐答案

if @individual.previous_changes.present?

Ruby-on-rails相关问答推荐

如何在rails 7中禁用Simple_Form验证

使用FactoryBot测试ActiveStorage的RSpec请求规范

select2 未应用于所有下拉列表;它只适用于数据的最后一条记录

为什么 Image_tag 产生images/...而不是assets/....?

Rails:关于yields

如何从设计中删除/禁用注册

Rails 中的转义值(类似于 mysql_real_escape_string())

在 Rails 3.2 中更新多行

如何为 Rails SimpleForm Select 框设置默认 Select 值

在 Rails 中将表格行变成链接

如何在 rspec 测试中输出变量?

Rails Active Admin css 与 Twitter Bootstrap css 冲突

Rails:仅在创建时验证,或者当字段不为空时在更新时验证

如何从邮箱中获取域

是否可以使用 Rails 迁移重命名索引?

如何从另一个调用 Capistrano 任务?

t.belongs_to 在迁移

Rails:在 link_to 中保留 GET 查询字符串参数

Rails:如何在rails中使用dependent::destroy?

我如何为 Rails3/Bundler 供应 gems