Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用两种不同格式渲染属性_Ruby On Rails - Fatal编程技术网

Ruby on rails 使用两种不同格式渲染属性

Ruby on rails 使用两种不同格式渲染属性,ruby-on-rails,Ruby On Rails,我有一个对象,它的属性在过去几年中一直使用降价语言,最近该属性现在切换到使用html。如何使用标记语言呈现所有过去的活动描述,如何使用.html\u safe呈现所有新的活动描述 Html_安全 <h6 class="text-muted" itemprop="description"> <%= @campaign.product_description.html_safe %> </h6> 降价 <h6 class="text-muted

我有一个对象,它的属性在过去几年中一直使用降价语言,最近该属性现在切换到使用html。如何使用标记语言呈现所有过去的活动描述,如何使用.html\u safe呈现所有新的活动描述

Html_安全

<h6 class="text-muted" itemprop="description">
    <%= @campaign.product_description.html_safe %>
</h6>

降价

<h6 class="text-muted" itemprop="description">
  <%= Campaign::Format @campaign.product_description, {render_html: true} %>
</h6>

我怎样才能同时渲染这两个呢?这样,对于所有旧的活动,它呈现标记,对于新的活动,它呈现html。当我在同一行中包含html_safe和标记时,它只会从WYSIWYG编辑器中执行标记,而不会执行html。

几个选项:

  • 为您的活动添加一个标志。类似于
    的东西使用html\u描述
    。相应地设置其值。然后在视图中打开它
  • 执行数据迁移,并用新的html格式替换所有旧格式的描述
    在这两者之间,我选择后者。

    我的第一个想法是将所有旧记录转换为新格式,然后回填数据库

    否则,在
    app/helpers/application\u helper.rb
    中创建一个视图帮助器方法,类似于:

    def description_text campaign
      if campaign.created_at > Time.new(2019,3,1).in_time_zone
        campaign.product_description.html_safe
      else
        Campaign::Format campaign.product_description, {render_html: true}
      end
    end
    
    请注意,如果您更喜欢字符串中的HTML而不是基于时间的内容,那么您可能还可以实际检测字符串中的HTML,例如:

    def description_text text
      if text.starts_with? "<html" # or whatever
        # ...
    
    def description_text
    
    如果text.u以开头?“为什么您更喜欢选项2?选项1似乎更简单。@Ronniewisenhower:是吗?基本工作量是一样的,您必须遍历所有记录并做一些事情(设置标志值或更改描述)。但是使用选项1,您的视图更复杂,您的db有一个额外的列(您必须首先部署它。这取决于您的数据库大小,这可能意味着一些停机时间)我看到了它的好处。这本身可能是一个全新的问题。如何将标记转换为html。我将一些标记放入其中并按预期工作。如何使用Ruby做到这一点。@Ronniewisenhower:我如何将标记转换为html“-嗯?这不正是您在视图中所做的吗?所以在迁移中执行它。是的,忽略最后一个问题。谢谢。它非常有效。