Getting syntax highlighter to work with my rails blog - ruby-on-rails

I am trying to use Syntax Highlighter to make my embedded code look great. I can do it on a html file but when I try on my rails blog it doesn't work.
My gut tells me it is something to do with my controller:
<pre class="brush: ruby;">
puts "Do you use Facebook? (Y/N)"
</pre>
<p>
<b>Entry:</b>
</p>
<%=sanitize #thought.entry %>
But the bit of html at the top is still running just fine...
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<script src="/assets/shCore.js?body=1" type="text/javascript"></script>
<script src="/assets/shBrushRuby.js?body=1" type="text/javascript"></script>
<link href="/assets/shCoreDefault.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript">SyntaxHighlighter.all();</script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="1OAMJ6dGJ8c7CHunEe9IBkNrcIJwrfYdwtskSFRGvUQ=" name="csrf-token" />
</head>
<body>
<p id="notice"></p>
<p>
<b>Title:</b>
Facehook
</p>
<p>
<b>Entry:</b>
</p>
<pre class="brush: ruby;">
puts "Do you use Facebook? (Y/N)"
facehooked = gets
if facehooked == "Y" #upper- or lower-case?
puts "How many friends do you have?"
friends = gets
if friends > 30
puts "I asked how many friends you have. Not how many friends you have on Facebook. You are Facehooked."
elsif friends == 0
puts "I am sorry to hear that."
else
#How sociable are you in the real world?
#TODO - data-type checking
puts "Please answer the following questions with a number:\nHow many people would you like to see on a weekly basis?"
buddies = gets
puts "How many people do you happen to see on a weekly basis?"
acquaintances = gets
puts "How many people that you do not like do you have to see on a daily basis?"
forced = gets
puts "How many people would you love to see on a daily basis?"
true_friends = gets
people = buddies + acquaintances + forced + true_friends
#ratios
true_friends_to_buddies = true_friends / buddies
buddies_to_acquaintances = buddes / acquaintances
true_friends_to_acquaintances = true_friends / aquaintances
puts "How many 'friends' do you have on Facebook?"
facehooks = gets
if people == 0
puts "You are ninja. You drift among this world without a connection. Please do not kill me."
elsif people >= 500
puts "You are one social motherfucker!"
else
end
end
elsif facehooked == "N"
puts "You do not have a hook in your face. Congratulations."
else
puts "That question required a yes or no answer."
end
puts "You should follow #ricburton on Twitter - he likes talking to new people, making acquaintances and building friendships."
</pre>
Edit |
Back
</body>
</html>
Any help would be much obliged!

Any help would be much obliged!
For syntax highlighting on rails, i would recommend using using prism.js via the gem highlighting
The integration is extremely simple. You can read more on how to integrate with rails here https://github.com/ytbryan/highlighting

Related

meta tag og:image for facebook sharing rails app

I have a page with article, and I can set from back-end an image for the article. In the show action I would like to obtain the meta tag og:image to share the article in facebook also with the image.
I show the image with:
<%= image_tag #article.image_url, :class => "img-fluid" %>
but I am not able to have the image also in facebook post automatically when I copy and paste the url in facebook.
Do you have any suggestion?
Thank you in advance.
According to this article here, you need a bit more setup.
https://www.lewagon.com/blog/setup-meta-tags-rails
Create default meta tags in config/meta.yml and load them in an initializer:
meta_product_name: "Product Name"
meta_title: "Product name - Product tagline"
meta_description: "Relevant description"
meta_image: "cover.png" # should exist in `app/assets/images/`
# config/initializers/default_meta.rb
# Initialize default meta tags.
DEFAULT_META = YAML.load_file(Rails.root.join("config/meta.yml"))
You can use them like this in the application.html.erb
<title><%= meta_title %></title>
<meta name="description" content="<%= meta_description %>">
<!-- Facebook Open Graph data -->
<meta property="og:title" content="<%= meta_title %>" />
<meta property="og:type" content="website" />
...
I shortened it but see the article for the full code.
For dynamic titles or images use Rails' content_for method and create a helper method:
<!-- app/views/any_model/any_view.html.erb -->
<% content_for :meta_image, image_path(#any_model.photo.path) %>
And the helper would look like this:
# app/helpers/meta_tags_helper.rb
module MetaTagsHelper
def meta_image
meta_image = (content_for?(:meta_image) ? content_for(:meta_image) : DEFAULT_META["meta_image"])
# little twist to make it work equally with an asset or a url
meta_image.starts_with?("http") ? meta_image : image_url(meta_image)
end
end
Again, shortened, but you'll find the full code in the source.

Star ratings wrong on Google using Schema

I'm trying to impliment the "Review ratings" schema on my Google listings.
My "total score" structure is as follows:
<div class="span7" itemscope itemtype="http://schema.org/Place">
<div class="stars" onclick="document.location.href='http://www.chambresdhotes.org/cgi-bin/links/review.cgi?ID=65995'" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<span class="badge badge-success">5 avis</span>
<meta itemprop="bestRating" content="10" />
<meta itemprop="worstRating" content="8" />
<meta itemprop="ratingValue" content="9.2" />
<meta itemprop="ratingCount" content="5" />
</div>
</div>
Then, for each of the reviews on that page - I also have:
<div class="reviewBox" itemscope itemtype="http://schema.org/Review">
<meta itemprop="author" content="Patrice" />
<meta itemprop="itemreviewed" content="Les Collinades" />
<div class="reviewRating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
<span class="link-rating"><span class="rating-5"></span></span>
<meta itemprop="bestRating" content="5" />
<meta itemprop="ratingValue" content="5" />
</div>
</div>
This all shows up OK when doing a schema check on the Google tool here:
https://search.google.com/structured-data/testing-tool#url=http%3A%2F%2Fwww.chambresdhotes.org%2FDetailed%2F65995.html
The "overall" ranking / number of reviews etc:
...and then the individual review scores:
However, when you go here its wrong:
https://www.google.fr/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:www.chambresdhotes.org+Detailed%2F65995.html
I'm a bit baffled as to why its doing this. What am I missing?
Thanks!
It looks like you've filled out bestRating and worstRating with the best and worst ratings your business has received. However, these are intended to be used to define the best possible and worst possible ratings.
Going from your example, I assume your customers can rate your business on a scale of 1 to 10. As such, bestRating would have to be 10 and worstRating would have to be 1.
You then have to calculate the average overall rating yourself, and put that under ratingValue

Rails Conditional Facebook OG Tags

I have a Rails personality test app that uses AJAX to render the questions and show the results.
I have been attempting to share personality test results via Facebook by sending the variable to one of my dashboard Rails controller to dynamically create tags.
However, the variable is nil when the page is first loaded (which causes an error), so I fixed it by adding a conditional (in application.html.erb):
<% if #cat %>
<meta property="og:title" content="You got <%= #cat.name %>!"/>
<meta property="og:image" content="<%= #cat.picture %>" />
<meta property="og:description" content="<%= #cat.description%>" />
<meta property="og:url" content="https://mysite" />
<meta property="fb:app_id" content= "<%=ENV['FACEBOOK_APP_ID']%>" />
<meta property="og:type" content="website">
<% end %>
Facebook does not recognize the tags when I checked the sharing debugger. I also tried initializing with another object (home controller):
def index
if params.has_key?("cat")
#cat = Cat.find_by(name: params["cat"])
else
#cat = Cat.find_by(name: "Colonel Meow")
end
end
With this, Facebook does not render the new value, but renders the old value instead. I put a debugger in my application.html to make sure the variables were changing (they did). Facebook did not refresh though.
Is there a way to use a conditional when assigning the tags? Or to refresh the tags so Facebook uses the new variable?

og:image meta tag in ruby on rails

I have some dynamic images defined as follows on my page:
<%= image_tag(#recipe.image.url(:fixed), :class => 'recipe_image_show') %>
I am looking for a way to define og:image with this code. The og:description code seen below works perfectly:
<meta property="og:description" content= "<%= #recipe.description %>" />
But if I use the code below then it doesn't want to work:
<meta property="og:image" content='<%= image_tag(#recipe.image.url(:fixed), :class => 'recipe_image_show') %>'/>
Any suggestions? I realised I have used single quotes here but using double quotes for some reason results the page to disregard the '/>' part of the meta tag and display it as text.
As per the chat discussion, OP is hosting the images on S3.
So, define the meta tag as below:
<meta property="og:image" content='<%= #recipe.image.url(:fixed) %>'/>
This will be evaluated to:
<meta property="og:image" content='https://s3....amazonaws.com/path/to/image.jpg'/>
NOTE:
If image was stored on the your own application server then you could use it as:
<meta property="og:image" content='<%= image_url #recipe.image.url(:fixed) %>'/>
if you are on rails 6 with wepback this one worked for me
- meta 'og:image': "#{asset_pack_url "static/image.webp"}"
check asset_pack_url on github for reference
your image path may vary, lookup manifest.json for this manner
Use the tool https://developers.facebook.com/tools/debug/
Click "Scrape Again" to reload Facebook Cache
Use full path with image_url("example.png")

Rails - failing to set open graph url correctly

I am developing a Rails application and I wish to use the open graph protocol so that on the "articles" page perople can link to social media (facebook, twitter, and google+) to recommend/like etc the article.
As part of the this I endevour, unsuccessfully to set og metatags for the url, within the head section of the page. From the code partial ogmeta.html.erb
<meta property="og:title" content='<%= meta[:title] || "DrillInvestor" %>'>
<meta property="og:type" content='<%= meta[:type] || "website" %>'>
<meta property="og:image" content='<%= meta[:image] || "http://www.whatever.com/image.jpg" %>'>
<meta property="og:url" content='<%= meta[:url] || "post_url(#post)" %>'>
Which when I view the page source I see
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<meta property="og:title" content='DrillInvestor'>
<meta property="og:type" content='website'>
<meta property="og:image" content='http://www.whatever.com/image.jpg'>
<meta property="og:url" content='post_url(#post)'>
What I am trying to pass as the url I can see from a print statement in the posts controller
puts "==================="
puts "url = " + post_url(#post).to_s
I see url = http://quiet-fortress-3338.herokuapp.com/posts/drill-01-more-updates which is what I am want, in this case, to see as the og:url.
I am a recovering COBOL pogrammer and sometimes (well perhaps more than that) lose my way with this sort of stuff.
If I hardcode the og:url I get the appropriate page. I have tried a numer of dfferent things including#{} enclosing the post_url(#post) etc.
Thanks in advance and any help welcome
Pierre
You need to get rid of the quotes around "post_url(#post)". Just change it to post_url(#post). Note that in your print statement you don't have it enclosed in quotes and it works.
As an aside, you don't need to call to_s in either case - post_url(#post) returns a string.

Resources