如何运行使用 strip_tags 的数据修改迁移?

发布于 2024-08-16 17:12:55 字数 734 浏览 4 评论 0原文

我正在向现有表添加一个新列 summary。它将包含 body 列中 HTML 的纯文本片段。

我想在运行迁移时为所有现有电子邮件创建摘要。但是,我不知道如何在迁移中使用 strip_tags。

到目前为止,这是我所拥有的:

class AddSummaryToEmails < ActiveRecord::Migration
  self.up
    add_column :emails, :summary, :string, :limit => 100

    Email.reset_column_information
    Emails.all.each do |email|
      email.update_attributes(:summary => strip_tags(email.body))
    end
  end

  ...

end

当然,这是行不通的: #的未定义方法“strip_tags”

如何在迁移中访问 strip_tags 方法?我知道我可以运行正则表达式或其他此类解决方法,但我仍然热衷于找出如何执行此操作以供将来使用。

谢谢

I'm adding a new column, summary, to an existing table. It will contain a plaintext snippet of the HTML from the column body.

I want to create summaries for all existing emails when I run my migration. However, I can't figure out how to use strip_tags within my migration.

Here's what I have so far:

class AddSummaryToEmails < ActiveRecord::Migration
  self.up
    add_column :emails, :summary, :string, :limit => 100

    Email.reset_column_information
    Emails.all.each do |email|
      email.update_attributes(:summary => strip_tags(email.body))
    end
  end

  ...

end

Of course, this doesn't work:
undefined method 'strip_tags' for #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb6e29be8>

How can I access the strip_tags method within my migration? I know I can run a regexp or another such workaround but am still keen to find out how to do this for future usage.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡绿茶香 2024-08-23 17:12:55

由于 strip_tags 是一个 ActionView 方法,并且您的迁移继承自 ActiveRecord,因此它看不到 ActionView 方法。

不过,您可以通过这种方式获取它们:

def self.up
  ActionController::Base.helpers.strip_tags("<b>TEST</b>")
end

如果您尝试包含 ActionView 变体,您将得到未定义的方法“full_sanitizer”,因为您需要扩展类方法,等等。更多的是痛苦。

Since strip_tags is an ActionView method and your migration inherits from ActiveRecord, it can't see the ActionView methods.

You can get to them this way, though:

def self.up
  ActionController::Base.helpers.strip_tags("<b>TEST</b>")
end

If you try including the ActionView variant, you'll get undefined method 'full_sanitizer' because you need to extend the class methods, and so on. Much more of a pain.

你在我安 2024-08-23 17:12:55

因为 strip_tags 超出了范围。您无权访问 ActionView::Helpers::SanitizeHelper。
请参阅相关问题

Because strip_tags is out of scope. You don't have access to ActionView::Helpers::SanitizeHelper.
See related question

感悟人生的甜 2024-08-23 17:12:55

wesgarrison 的解决方案不适用于 Rails 2.3.5 版本,因此为了使其工作,我必须直接从迁移中的 HTML::FullSanitizer 模块调用 HTML sanitize 方法,例如this:

def self.up
  HTML::FullSanitizer.new.sanitize('<p>Test</p>')
end

希望这可以帮助那些和我有同样问题的人。

wesgarrison's solution didn't work for me with Rails 2.3.5 version, so in order to make it work I had to directly call HTML sanitize method from the HTML::FullSanitizer module in the migration like this:

def self.up
  HTML::FullSanitizer.new.sanitize('<p>Test</p>')
end

Hope this helps someone who has the same problem as me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文