Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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 Httparty显示HTML_Ruby On Rails_Json_Httparty - Fatal编程技术网

Ruby on rails Httparty显示HTML

Ruby on rails Httparty显示HTML,ruby-on-rails,json,httparty,Ruby On Rails,Json,Httparty,我正在使用httpartygem将wp-json日期从wordpress站点获取到我的Rails应用程序 下面是我的控制器,它获取json信息: require 'httparty' require 'json' class ConnectionController < ApplicationController respond_to :json def index end def thirty_days get_thirty_days end pr

我正在使用httpartygem将wp-json日期从wordpress站点获取到我的Rails应用程序

下面是我的控制器,它获取json信息:

require 'httparty'
require 'json'

class ConnectionController < ApplicationController
  respond_to :json

  def index
  end

  def thirty_days
    get_thirty_days
  end

  private

  def query_wordpress_category(wordpress_category)
    @response = HTTParty.get("http://thriveconnection.com/wp-json/posts?filter[category_name]=#{wordpress_category}&filter[order]=date")
  end

  def get_thirty_days
    query_wordpress_category("30-days")
    @thirty_days = JSON.parse(@response.body)
  end

end
需要“httparty”
需要“json”
类ConnectionController
下面是它显示的内容:

以下是我的看法:

<% @thirty_days.each do |article| %>
    <div class="row">
      <div class="col-md-6">
        <a href="<%= article['link'] %>" target="_blank">
          <img src="<%= article['featured_image']['source'] %>" alt="" class="img-responsive thumbnail" />
        </a>
      </div>
      <div class="col-md-6">
        <strong class="title-font">
          <a href="<%= article['link'] %>" target="_blank">
            <%= article['title'] %>
          </a>
        </strong>

        <span class="small-font">Published Date <%= article['date'] %></span>
        <br /><br />
        <%= article['excerpt'] %>
        <br /><br />
        <a href="<%= article['link'] %>" target="_blank">
          <i class="fa fa-bookmark-o"></i>Read the Article
        </a>
      </div>
    </div><!-- END .row -->
    <% end %>



出版日期





这是我的问题,wordpress的摘录显示为html。我如何显示它,使HTML标记不会显示出来。另外,有没有办法将日期格式化为mm/dd/yyyy格式?

您需要将不希望转义的字符串标记为允许Rails视图处理器知道不转义的字符串。请注意,如果您没有逃脱它们,那么您将在您检索的字符串中包含恶意数据包,从而使您的用户面临其他站点的各种黑客企图

所以,例如

<%= article['title'].html_safe %>

同样对于我使用的日期格式

<%= Date.parse(article["date"].strftime("%m/%d/%Y") %> 


在视图中显示解析后的json数据是否有另一种安全的方式,而不是html安全或原始的方式?没有特别的原因,我只想使用最安全的方式和最佳实践。哦,对了,你的意思是通过不信任html来缓解我提到的问题?最好的办法是写一个消毒剂,真棒,正是我想要的。非常感谢@smarthy!