【问题标题】:Rails: Insert a lot of HTML code through migration to the databaseRails:通过迁移到数据库插入大量 HTML 代码
【发布时间】:2013-10-22 06:02:01
【问题描述】:

那么问题来了:如何使用迁移将大量 HTML 代码(大约 200 行代码)插入到数据库中? 我试图这样做:

class AddManyruToPages < ActiveRecord::Migration
include ActionView::Helpers
def up
      Page.create(
      slug:'manyru',
      content:
      <<-HTML
      <div class="manyru-wrapper">
        <div class="many-header">
          <div class="many-content">
            <div class="logo">
            #{image_tag "/images/manyru/many-logo.png"}
            </div>                  
          </div>                     
         </div>
      </div>
      HTML
      )
end

def down
      Page.find_by_slug('manyru').destroy
end
end

但它没有用。我唯一知道的是插入后立即发生回滚,我不知道为什么? 如果我应该添加更多信息,请告诉。

【问题讨论】:

    标签: mysql ruby-on-rails ruby database-migration


    【解决方案1】:

    回滚通常意味着您的 create 方法由于您的模型上指定的验证而失败。

    将结果分配给某个变量:

    p = Page.create ...
    

    然后检查错误:

    puts p.errors.full_messages.inspect
    

    【讨论】:

    • 抱歉耽搁了埃德加斯。我确实像你说的那样,但它只向我展示了这个:未定义的局部变量或方法'config',我认为问题在于我的代码中的'image_tag'助手。你怎么看?
    • 然后我需要查看 HTML 内部并知道错误在哪里。
    • 我刚刚发现我什至无法在 rails 控制台中使用 image_tag。同样的错误。也许我做错了什么
    • Helper 仅在写入 'image_tag("")' 时有效。这有点奇怪
    • 不知道是什么问题,试试不加模块直接调用helper,像这样:ActionController::Base.helpers.image_tag("manyru/many-logo.png") 指定路径的时候也跳过图片文件夹,assets libs应该考虑进去。
    猜你喜欢
    • 2011-10-12
    • 2020-12-07
    • 2010-12-05
    • 2013-07-15
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多