Httparty displaying HTML - ruby-on-rails

I am using httparty gem to get wp-json date from a wordpress site to my Rails application.
Here is my controller that gets the json info:
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
Here is what it gets displayed: http://d.pr/i/1jMEq
Here is my view:
<% #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 %>
Here is my problem, the excerpt from wordpress gets displayed as an html. How can I display that so the HTML tags don't show up. Also, is there any way I can format the date to mm/dd/yyyy format?

You need to mark the strings that you don't want escaped as html_safe which allows the Rails view processor to know not to escape them. Be warned, if you don't escape them then you open your users up to various hack attempts by the other site by including a malicious packet in the strings you retrieve.
So, eg.
<%= article['title'].html_safe %>

Also as for date format I used
<%= Date.parse(article["date"].strftime("%m/%d/%Y") %>

Related

Youtube Data Api : How to add redirect link to each videos from Youtube trending

I am working on an app where I display current trending videos on YouTube (thumbnail, video title and channel title) via YoutubeDataApi. I would like to access to the correct URL whenever I click on the thumbnail or the title of a trending video.
This is the code I'm using in my controller:
# frozen_string_literal: true
class WelcomeController < ApplicationController
def index
youtube_data_api = YoutubeDataApi.new.client
#result, #errors = youtube_data_api.list_videos 'snippet, id', chart: 'mostPopular', region_code: 'FR', max_results: '50'
end
end
And this is the code I use in my view:
<div style="margin:0 auto; padding:20px 0;">
<% #result.items.each do |video| %>
<a href="https://www.youtube.com/watch?v=#{video_id}" target="_blank" style="color:black"><img src="<%= video.snippet.thumbnails.default.url %>" height="90" width="120">
<div id="channel_title">
<u><b><%= video.snippet.channel_title %></u></b>
</div>
<div id="video_title" style="margin-bottom: 50px;">
<i><%= video.snippet.title %></i>
</div>
</a>
<% end %>
Gem used in this app: gem 'google-api-client', '>0.7'
Could anyone please help me? Thanks in advance
Which gem are you using so we can see what you have to work with a little better.
Just take this with a grain of salt but your image has a src "<%= video.snippet.thumbnails.default.url %>"
Does that url point to the video url? If it does just change your href value to that video.snippet.thumbnails.default.url.
Currently with that href all of your videos will open a new tab to "https://www.youtube.com/feed/trending"
Is there a url on video.snippet (video.snippet.url) ?
Oh also you will need to wrap an around both the title and the thumbnail so that when you click on them it will link. With your loop you are getting the data for each individual video so if you can get the right url you just need to put links around everything and you should be good.
EDIT:
I think I have the answer for you. You are already sending off to retrieve the id's of the videos to the api so all you have to do to get them is do, `video.id
v=#{video.id} instead of video_id unless you make another variable called that and set it equal to video.id.
in your view loop:
<div style="margin:0 auto; padding:20px 0;">
<% #result.items.each do |video| %>
<a href="https://www.youtube.com/watch?v=<%= #{video.id} %>" target="_blank" style="color:black"><img src="<%= video.snippet.thumbnails.default.url %>" height="90" width="120">
<div id="channel_title">
<u><b><%= video.snippet.channel_title %></u></b>
</div>
<div id="video_title" style="margin-bottom: 50px;">
<i><%= video.snippet.title %></i>
</div>
</a>
<% end %>
So just simply the variable injected in your a href changes from video_id to video.id. However what youtube gives you is simply the video id not the full link, as you already noticed. With that slight ruby variable change you should be good to go! :)
Oh forgot to mention the ruby won't show up unless you throw in an erb like tag like shown above.
A possibly nicer way of doing the same thing would be to do:
<%= link_to "https://www.youtube.com/watch?v=#{video.id}", style="color: black;", target="_blank", style="color: black;" do %>
.... (stuff you are wrapping)
<% end %>
So altogether:
<div style="margin:0 auto; padding:20px 0;">
<% #result.items.each do |video| %>
<%= link_to "https://www.youtube.com/watch?v=#{video_id}", style="color: black;", target="_blank", style="color: black;" do %>
<img src="<%= video.snippet.thumbnails.default.url %>" height="90" width="120">
<div id="channel_title">
<u><b><%= video.snippet.channel_title %></u></b>
</div>
<div id="video_title" style="margin-bottom: 50px;">
<i><%= video.snippet.title %></i>
</div>
<% end %>
<% end %>
https://api.rubyonrails.org/v5.2.1/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Cheers

facebook comments plugin to show comments dynamically

i use facebook comments plugin https://developers.facebook.com/docs/plugins/comments/
for every page in my site
and i show the facebook comments by fetching from json request
https://graph.facebook.com/comments/?ids=abc.com/
so when i update comment in facebook plugin, i would like to get the comments in my site to get dynamically updated
my code in controller
#fb_res=HTTParty.get('https://graph.facebook.com/comments/?ids=abc.com/')
fb_json=JSON.parse(#fb_res)
#fb_comment = fb_json["abc.com/"]["comments"]["data"].map {|x| [x["from"]["id"],x["message"],x["from"]["name"]]}
my views :
<% #fb_comment.each do |fb| %>
<li>
<div class="pic_commnt">
<div class="pic">
<div class="pic_image">
<img src="http://graph.facebook.com/<%= fb[0].to_s %>/picture?type=square" alt="" />
</div>
<span class="pic_name"><%= fb[2].to_s %></span>
</div>
<div class="pic_comments">
<p>
"<%= fb[1].to_s %>"
</p>
</div>
</div>
</li>
<% end %>
i would like to get this section updated dynamically

wysihtlm5 form submit and validation

I'm having trouble implementing the wysihtlm5 editor using the wysihtml5-rails gem: https://github.com/NARKOZ/wysihtml5-rails
First of all, my html tags are not saved to the database. Then if the form submit fails, the text_area is only filled with plain text. How can i make sure, the html tags are sent to the server in the right way?
In the debug output i can see that the description is arriving in the controller without html tags:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"fW5FMxr/sgNkLUowLT2E0UfIXtFbXvkOubPYM0GJm0I=", "description"=>"sdfgsdfg"}, "_wysihtml5_mode"=>"1"}
application.js:
//= require wysihtml5
//= require parser_rules/advanced
In my view:
<%= form_for #auction do |f| %>
<div class="control-group">
<label class="control-label" for="auction_days">Beschreibung</label>
<div class="controls">
<div id="wysihtml5-toolbar" style="display: none;">
<div class="btn-group">
<a data-wysihtml5-command="bold" title="CTRL+B" class="btn"><i class="icon-bold"></i></a>
<a data-wysihtml5-command="italic" title="CTRL+I" class="btn"><i class="icon-italic"></i></a>
<a data-wysihtml5-command="underline" title="CTRL+U" class="btn"><i class="icon-underline"></i></a>
</div>
<div class="btn-group">
<a data-wysihtml5-command="formatBlock" data-wysihtml5-command-value="h1" class="btn">H1</a>
<a data-wysihtml5-command="formatBlock" data-wysihtml5-command-value="h2" class="btn">H2</a>
</div>
<div class="btn-group">
<a data-wysihtml5-command="insertUnorderedList" class="btn"><i class="icon-list-ul"></i></a>
<a data-wysihtml5-command="insertOrderedList" class="btn"><i class="icon-list-ol"></i></a>
</div>
<a data-wysihtml5-command="createLink" class="btn"><i class="icon-link"></i> Link</a>
<!--<a data-wysihtml5-action="change_view">switch to html view</a>-->
<br /><br />
<div data-wysihtml5-dialog="createLink" style="display: none;" class="alert alert-info input-xxlarge">
<label><strong>Link einfügen:</strong></label>
<input type="text" data-wysihtml5-dialog-field="href" value="http://" class="span3">
<a data-wysihtml5-dialog-action="save" class="btn">OK</a> <a data-wysihtml5-dialog-action="cancel" class="btn">Abbrechen</a>
</div>
</div>
<%= f.text_area :description, :id => 'wysihtml5-textarea', :label=>false, :class=>"input-xxlarge", :placeholder => "Beschreibung hier einfügen ..." %>
<script>
var editor = new wysihtml5.Editor("wysihtml5-textarea", {
toolbar: "wysihtml5-toolbar",
parserRules: "wysihtml5ParserRules"
});
</script>
</div>
<%= button_tag(type: 'submit', class: "btn btn-success") do %>
<i class="icon-ok icon-white"></i> Send
<% end %>
<% end %>
is the editor load properly? try inspect element in browser for the case when validation fail ,is because what you re saving is plain text, so it will just display plain text for you as well.
I saw there is a html view tag in the demo, maybe you try check whether it can be changed to html text or not.
Additionally:
I personally recommend the plain 1 without gem.
https://github.com/mindmup/bootstrap-wysiwyg
or ckeditor or tinymce :D

How can I display every 4th product with different markup?

I'm just starting out with Ruby from a java background. I'm trying to code a particular loop but can't figure out the right syntax:
Could someone help me troubleshoot this please? I'm trying to write a loop but use different css classes to change the style of each block.
Realise this is probably easy, but help is appreciated....
<%= #products.each do |product, i| %>
<% if i % 1 %>
<div class="items-row clearfix">
<div class="one_fourth">
<div class="item-thumb">
<img src="<%= image_path "thumb.png" %>" alt="" class="thumb" width="162" height="230" />
</div>
<p><%= product.name %></p>
<p class="bold">$79.95 AUD</p>
<p class="color-wrap">
<span class="color" style="background:#ddd;"></span>
<span class="color" style="background:#f9f9f9;"></span>
<span class="color" style="background:green;"></span>
<span class="color" style="background:red;"></span>
</p>
</div>
<% elsif i % 4 %>
<div class="one_fourth last">
<div class="item-thumb">
<img src="<%= image_path "thumb.png" %>" alt="" class="thumb" width="162" height="230" />
</div>
<p><%= product.name %></p>
<p class="bold">$79.95 AUD</p>
</div>
</div><!-- end row -->
<% else %>
<div class="one_fourth">
<div class="item-thumb">
<img src="<%= image_path "thumb.png" %>" alt="" class="thumb" width="162" height="230" />
</div>
<p>Item Name</p>
<p class="bold">$79.95 AUD</p>
</div>
<% end %>
<% end %>
You can do something like this using cycle:
<% products.each_slice(4) do |slice| %>
<div class="items-row clearfix">
<% slice.each_with_index do |product,i| %>
<div class="one_fourth <%= cycle("first", "second", "third", "fourth") %>">
<%= render product %>
<% if i != 3 %>
<p class="color-wrap">
<span class="color" style="background:#ddd;"></span>
<span class="color" style="background:#f9f9f9;"></span>
<span class="color" style="background:green;"></span>
<span class="color" style="background:red;"></span>
</p>
<% end %>
</div>
<% end %>
</div>
<% end %>
_product.html.erb:
<div class="item-thumb">
<img src="<%= image_path "thumb.png" %>" alt="" class="thumb" width="162" height="230" />
</div>
<p><%= product.name %></p>
<p class="bold">$79.95 AUD</p>
Rather than if/else statements and CSS markup in your views, you should probably externalize your CSS, make a few partials, and make use of ActionView::Helpers::TextHelper#cycle. For example:
<%= #products.each do |product| %>
<div class="<%= cycle('first', 'second', 'third', 'fourth') -%>">
<!-- 1. let the CSS class handle display differences -->
<!-- 2. render a partial based on #current_cycle -->
<%= render :partial => current_cycle %>
</div>
<% end %>
I'd say the mod's are your problem. It's never going to go into the first block as n%1 will always return 0. Try your logic out using irb to get you going.
You may want to try jQuery nth-child
I got part of the answer. Slight change of syntax to use "each_with_index" instead of just each.
Still trying to work out there's a lot of variations of when a product can be the last tile and require closing, first on row, first in set, last in a row.....
Thanks guys, I think I've answered my own question.

Build templates in views

In grails I do this:
<form:windowEntrada domain="serverPool">
<form:entrada domain="serverLocation" bean="${serverPoolInstance}">
<form:textField label="serverPool.name" name="name" />
<form:select label="serverPool.server" name="server" list="${Servers.list()}"/>
</form:entrada>
</form:windowEntrada>
The tag "form:textField" render something like:
<div class='field-name'>
<div class="title"><label for="name">Name</label>(* required)</div>
<div class="fieldcontent"><input type="text" name="name" value="smtg"></div>
<div class="error">If thereis an error on this field, the message appears here</div>
</div>
The tag "form:windowEntrada" render something like:
<div class='container'>
<h1>My title here</h1>
<div class="content">${body()}</div>
</div>
The tag "form:entrada" render something like:
<form action ... >
<....> --> messages stuff
${body()}
<div class="action-buttons">
<input type="submit" name="lala">
<input type="submit" name="lala2">
</div>
</div>
How can I do i rails using erb as my view tecnology?
Thank you.
I Have found the answer here: pathfindersoftware.com/2008/07/pretty-blocks-in-rails-views
Here's an example applying the tutorial:
In ERB:
<%= widget("mytitle", {:class => "widget-table"}) do %>
test test test
<% end %>
in the helper:
def widget(title, options = {}, &block)
options[:params] = params
options[:controller_name] = controller.controller_name
body = capture &block
html = render "shared/form/widget", :body => body,
:title => title,
:options => options
return html
end
and the template _widget.html.erb:
<div class="widget <%= options[:class] unless options[:class].blank? %>">
<% unless title.blank? %>
<div class="widget-header">
<span class="icon-layers"></span>
<h3><%= title %></h3>
</div>
<% end %>
<div class="widget-content">
<%= body%>
</div>
</div>
tks

Resources