Unable to parse JSON of crypted text - ruby

I updated to Ruby 2.4.2 which required me to update Gibberish to 2.0.
I am getting this error and not sure what is the culprit.
Completed 500 Internal Server Error in 157ms
Feb 20 10:34:14 my-staging app/web.1: ActionView::Template::Error (Unable to parse JSON of crypted text):
348: <% end %>
349: <li class="dropdown <%= 'active' if request.fullpath == "/es/account" %>">
350: <a aria-haspopup="true">
351: <i class="icon-user"></i> <%= current_user.email %>
352: <b class="caret"></b>
353: </a>
354: <ul>
Feb 20 10:34:14 my-staging app/web.1: config/initializers/mongoid_encryption.rb:15:in `decrypt'
I think the issue might be this gem, but not sure??
gem 'mongoid-encrypted-fields', :path => "./vendor/plugins/mongoid-encrypted-fields-1.2.2"

Related

Insert dynamic class assignment to a Rails link_to tag

I am trying to add a dynamic class to a link_to in Rails 7. I have the following code, but it's not inserting the appropriate content based on the request.env["PATH_INFO"]...
<ul>
<% %w[blog headlines network].each do |nav_link| %>
<%= added_class = request.env["PATH_INFO"] == nav_link ? "nav_current" : nil %>
<li class="btn"><%= link_to "#{nav_link}".capitalize, "/#{nav_link}", id: "nav_main", class: "btn btn_nav #{added_class}" %></li>
<% end %>
</ul>
However, the resulting HTML is:
<ul>
<li class="btn"><a id="nav_main" class="btn btn_nav " href="/blog">Bog</a></li>
<li class="btn"><a id="nav_main" class="btn btn_nav " href="/headlines">Headlines</a></li>
<li class="btn"><a id="nav_main" class="btn btn_nav " href="/network">Network</a></li>
</ul>
As you can see, the added_class doesn't get inserted. Any ideas?
request.env['PATH_INFO'] contains a leading / (at least on Rails 7; and IIRC on previous versions as well)
That means that if you are visiting localhost:3000/blog, request.env['PATH_INFO'] is /blog, not blog.
The following code should fix it:
<ul>
<% %w[blog headlines network].each do |nav_link| %>
<% added_class = request.env["PATH_INFO"] == "/#{nav_link}" ? "nav_current" : nil %>
<li class="btn"><%= link_to "#{nav_link}".capitalize, "/#{nav_link}", id: "nav_main", class: "btn btn_nav #{added_class}" %></li>
<% end %>
</ul>
Ok, so the following code does what I wanted it to. It injects a custom class into the link tag so that I can track the current button and style it differently... :
<ul>
<% %w[blog headlines network].each do |nav_link| %>
<% add_class = request.env['PATH_INFO'] == "/#{nav_link}" ? "nav_current" : "" %>
<li class="btn"><%= link_to "#{nav_link}".capitalize, "/#{nav_link}", id: "nav_main", class: "btn btn_nav #{add_class}" %></li>
<% end %>
</ul>
No Javascript necessary.

Issue using .erb to display date with Sinatra

I am using ruby .erb to display time in a div's span. However using something similar for date does not work.
<div id="comment_space">
<% #post.comments.each_with_index do |comment,date,posted,index| %>
<span class="above"><%= date = ('%Y-%m-%d') %> </span>
<div class="comment" id="msg_<%= index+1 %>">
<%= comment.usr_comment %>
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
</div>
<% end %>
<span class="above">
<%= date = ('%Y-%m-%d') %>
</span>
This is the part not working. However
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
works perfectly.
The first error was that "created_at" was not lked for the date and it has gone down hill from there. The date and time were defined in Javascript with timestamps in a Ruby ActiveRecord table.
<%= date = ('%Y-%m-%d') %> # Ehhhhhm
Ok, compare that to your other way of doing this.
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
See the difference?
If the "date" is Date class, you can do:
<%= date.strftime('%Y-%m-%d') %>
and it should work with no problem whatsoever.
If you do what you did, you say that the date is now '%Y-%m-%d'
date = ('%Y-%m-%d')
# => "%Y-%m-%d"

Set image in image-tag using rails 3.

I want to set one image in image tag using RoR. That image path is fetched from DB and it has to set inside image tag.I have done some coding but unable to display that image.It also showing the following error.
Error:
SyntaxError in Homes#home
Showing C:/Site/swargadwara_puri/app/views/homes/home.html.erb where line #209 raised:
C:/Site/swargadwara_puri/app/views/homes/home.html.erb:209: syntax error, unexpected $undefined
...",:height => "140",:border => \'0\',:class =>\'borderblackdi...
... ^
C:/Site/swargadwara_puri/app/views/homes/home.html.erb:223: syntax error, unexpected ')', expecting keyword_end
'); else
^
Extracted source (around line #209):
206: </div>
207: <div class="tpaddingdiv1">
208: <div class="col-md-4">
209: <div class="text-center bpaddingdiv1"><% image_tag("<%= #hcsy.D_Photo %>",:height => "140",:border => '0',:class =>'borderblackdiv' ) %></div>
210: <div class="fontweightbolddiv fontssizediv3 text-center bpaddingdiv1">Deceased Photo</div>
211: </div>
212: <div class="col-md-4">
Please check my below line.
<div class="text-center bpaddingdiv1"><% image_tag("<%= #hcsy.D_Photo %>",:height => "140",:border => '0',:class =>'borderblackdiv' ) %></div>
Please help me to resolve this error and display this image successfully.
Try this,
<div class="text-center bpaddingdiv1"><%= image_tag("#{#hcsy.D_Photo}",:height => "140",:border => '0',:class =>'borderblackdiv' ) %></div>
Changes are: <% image_tag... to <%= image_tag and "<%= #hcsy.D_Photo %>" to "#{#hcsy.D_Photo}"
Edited
change "<%= #hcsy.D_Photo %>" to "/#{#hcsy.D_Photo}"

How to output array via ECO in DocPad to build for example an image gallery?

I have a document with the following frontmatter:
---
layout: default
title: "A Gallery"
image:
- "image-1.jpg"
- "image-2.jpg"
- "image-3.jpg"
---
Now I want to build a list of images in my default-template like this.
<ul>
<li><img src="image-1.jpg" alt=""></li>
<li><img src="image-2.jpg" alt=""></li>
<li><img src="image-3.jpg" alt=""></li>
</ul>
I found this tutorial for javascript loops.
But how do I convert it to ECO/Coffeescript?
Something like this?
<ul>
<% gallery i + 1, for images i in #document.image[i]: %>
<li><%- #document.image[i] %></li>
<% end %>
</ul>
This is how I did it.
In your docpad.coffee file:
site:
pics:
myFirstGallery: [
{name: '/images/mypic.png' h: '180px' w: '100px'},
{name: '/images/mypic2.png' h: '180px' w: '100px'}
]
Then in your eco file:
<ul>
<% for pic in #document.site.pics.myFirstGallery: %>
<li>
<img src="#{pic.name}" width="#{pic.w}" height="#{pic.h}" />
</li>
<% end %>
</ul>
It should be something similar to that. I use Jade so I forget the exact CoffeeScript syntax. Let me know if I got something wrong. The for in syntax comes from CoffeeScript's website. I looked it up on Eco's ReadMe file.
I figured it out after seeing this code in »The Little Book on CoffeeScript« (GitHub Version).
for name, i in ["Roger the pickpocket", "Roderick the robber"]
alert "#{i} - Release #{name}"
For the sample above (see my question) you have to write this code.
<ul>
<% for image, i in #document.image: %>
<li>
<img src="<%- "#{image}" %>" />
</li>
<% end %>
</ul>
Now it works like charm.

Creating a Blank Rails 3.1 Templating Handler

What I want to do is make it just output the view itself, and ignore what Rails would normally think is embedded Ruby within the HTML.
For example:
<div class="current">
<div class="question">
<h3 id="homework_name"><%= homework.name %><h3 id="due">Due <%= homework.due %></h3></h3>
<h2 class="title">The Question:</h2>
<p id="question_contents"class="contents"><%= current_question.contents</p>
</div>
<div class="answer">
<h2 class="title">Your Answer:</h2>
<textarea class="contents" id="student_answer"><%= current_question.answer %></textarea>
</div>
</div>
I want an ActionView Template Handler to ignore all mentions of:
<%= homework.name %>
<%= homework.due %>
<%= current_question.contents %>
<%= current_question.answer %>
More specifically, it should ignore any tags starting with <% and ending with %>
If you need more info check out http://pastie.org/private/epsxnapldho6co2y0indg
Here you go https://gist.github.com/1144297
And use .html.lifo extensions for your templates.

Resources