Rails: wkhtmltopdf is not executable - ruby-on-rails-6

I am trying to get the wicked_pdf to work on localhost. The problem is, this error shows up, when trying to access it.
wkhtmltopdf is not executable
This is how I linked it
<%= link_to "Create PDF document", order_path(#order, format: :pdf) %>
This it the wicked_pdf inicializer
WickedPdf.config = {
exe_path: Gem.bin_path("wkhtmltopdf-binary", "wkhtmltopdf"),
}
And this is the controller
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "File",
template: "orders/show.html.erb",
layout: "pdf.html"
end
end
end
I am using Rails 6.

Related

flatpickr via rails importmap not being generated

Attempting to use flatpickr as a UI for setting a date field with Rails 7.0.1 and ruby 3.1. When clicking on the input field, the expected 2 months of calendars is not rendered & in fact, nothing is registered in the browser network tab.
After bin/importmap pin flatpickr rails pinned the library to the cdn in the importmap.rb file:
pin "flatpickr", to: "https://ga.jspm.io/npm:flatpickr#4.6.9/dist/flatpickr.js"
application.js has the initially generated import "controllers".
flatpickr_controller.rb was created in the javascript/controllers directory with
import { Controller } from "#hotwired/stimulus"
import flatpickr from "flatpickr"
export default class extends Controller {
connect() {
this.config = {
enableTime: false,
dateFormat: 'Y-m-d',
}
}
}
The view partial invokes:
<%= form_with model: promo do |f| %>
<%= f.text_field :date_from, placeholder: t(".select"), data: { controller: "flatpickr" } %>
<% end %>
while the controller sets
#promos = Promo.up_comings(60)
#promo = Promo.new
#next_promo = Promo.next
#dates_to_disable = #promos.pluck(:date_from)
Why is the calendar not rendering?
I am also confused as to where to place the different flatpickr options: in the controller, or straight into the form date element.
The issue is related to support for Stimulus 3. Pin as follows:
bin/importmap pin stimulus-flatpickr#beta
Also reference some css source, such as to the latest version
<link rel="stylesheet" href="https://ga.jspm.io/npm:flatpickr#4.6.9/dist/flatpickr.min.css">
otherwise the input field will be deemed as readonly...

Rails Variables inside the CSS file

I have CSS file that needs to be embedded by the user. Now for the user to customize the CSS file as he/she wishes (change the colors and font-size mainly) I have to store that information in database file, append that information to specfic CSS file and then enable that user to embed it.
I would get the id of the User that I wish to change. So color codes stored are basically extracted from,
User.find(params[:id]).main_color
# This would return => #fffff
This what my embed code looks like right now,
<link href="link.com/initial.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" data-user="18" src="link.com/some_file.js" />
Here data-user is the id of the user who is modifying the CSS file. (The IFrame is being embedded using the javascript file)
How should I modify my embed code and fetch the custom variables from my database in order to make the CSS cstomizable as the user wants?
Thanks in advance.
P.S I have thought of rewriting the CSS in the view(.html.erb file) and using !important there along with proper color codes selected by the user but that approach is not exactly very efficient.
I think the best way is to keep your script file at the server side, So you can easily modify your script depending on user database options
for example:
place a new route in routs.rb
post '/users/modify_style', :to => 'users#modify_style', :as => modify_style
then allow the route to respond to user by JavaScript file, for example if the user needs to change the main color of HTML template using a button or a link; so the form button in template may look like:
<%= button_to 'apply style', modify_style_path, :data => {:user_id => current_user.id}, :remote => true %>
or
<%= link_to 'apply style', modify_style_path(:user_id => current_user.id), :method => :post, :remote => true %>
so remote: true option will allow sending data to server through AJAX without refreshing the page.
at users_controller.rb
def modify_style
user = User.find(params[:user_id])
#main_color = user.main_color
respond_to do |format|
format.js { render 'some_file.js.erb' }
end
end
and in your js file:
$('#some_element_id_to_change_color_for').css('color', '<%= #main_color %>')
or you can add modify_style action code directly to the original action which load your page to appy user style once the page is loaded.
I would suggest you to directly use it in the erb file something like
my.html.erb
<html>
<!-- my html -- >
</html>
<style>
<%= my_css.html_safe %>
</style>

Middleman and Sinatra Contact Form rendering template

I'm trying to create a middleman site with a contact form using sinatra and pony gem
I'm having problems getting middleman and sinatra to play nicely together
THE ISSUE:
"
My Contact From is not rendering my header and footer and also does not like it when I try to include view helpers like = link_to tags When I try that I get this error
undefined method `link_to' for
Its also not picking up my stylesheets
"
Heres my Mailer.rb get method
class MailForms < Sinatra::Base
set :views, Proc.new { File.join(root)}
get '/' do
haml :contact, :layout => :'/contact'
end
end
and it lives in source/mailer.rb
when I go to website.com/contact.hml
I get the form but it has no header or footer all my other pages have them and is working
I can't use = link_to tags or image tags i get an undefined method error
Its also not picking up my styles
heres my config.rb
require 'source/mailer'
map('/contact.html') do
run MailForms
end
set :css_dir, 'stylesheets'
set :js_dir, 'javascripts'
set :images_dir, 'images'
set :relative_links, true
configure :build do
activate :relative_assets
end
heres my layout.haml with my stylesheet link
!!!
%html
%head
%meta{charset: "utf-8"}
%meta{content: "IE=edge,chrome=1", "http-equiv" => "X-UA-Compatible"}
%title= current_page.data.title || "Auto Tint Specalist"
%link{href: "//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css", rel: "stylesheet"}
= stylesheet_link_tag "normalize", "all"
%script{src: "//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"}
= javascript_include_tag "all"
%body{class: page_classes}
= partial 'layouts/header'
= yield
= partial 'layouts/footer'
all my styles are in all.scss in the stylesheets folder

Getting 'active_link_to' to work properly with Pagination

I am trying to get active_link_to to work properly when I use pagination. I have 3 nav-tabs and they all work fine when selected, however, when I click on my pagination button to view more records my current nav-tab losses it's active status (style). What steps do I need to fix this issue?
Also 1 more minor issue if somebody doesn't mind. I'm using a log-in screen to get me to my main layout. When I log in, none of the nav-tabs are active. How do I render this? If I click on one of them it shows active but I want the 1st tab to display active by default. Again, I am using active_link_to.
Thank You.
TW
Here is my updated post with Code:
In my Gemfile I have
gem kaminari
gem active_link_to
In my projects_controller.rb
#projects = Kaminari.paginate_array(#projects).page(params[:page]).per(10)
In my index.html.erb
At the end of my page
</table>
</div>
<%= paginate #projects %>
In my projects_helper.rb
def active_link_to(text, link)
content_tag(:li, :class => request.fullpath == link ? 'active' :nil) do
link_to text, link
end
end
In my Layout application.html.erb
<ul class="nav nav-tabs">
<li><%= active_link_to("Active Projects", projects_path) %></li>
....
....
</ul>
The problem is you are trying to match the link to the fullpath entirely and your fullpath will sometimes contain a query string which will break the fullpath == link if you don't also include the query string in your link.
One possible solution would be to match a substring of the fullpath, something like this:
def active_link_to(text, link)
content_tag(:li, :class => request.fullpath.starts_with?(link) ? 'active' :nil) do
link_to text, link
end
end
Or even using Ruby String's [] method to ask if a substring is present in the string:
def active_link_to(text, link)
content_tag(:li, :class => request.fullpath[link].present? ? 'active' :nil) do
link_to text, link
end
end
You can also try a Gem that provides the method you want: https://github.com/comfy/active_link_to
It uses a more comprehensive method of matching paths.

Add custom template in CKEditor 3 using classic ASP

I have a website that uses classic ASP to render settings for CKEditor 3.
On the serverside I have:
<%
Dim Editor
Set Editor = New CKEditor
Editor.basePath = "/ckeditor/"
Editor.Config("contentsCss") = "/Styles/Editor.css"
Editor.Config("templates_files") = "/templates/news.js"
Editor.Config("width") = 570;
Editor.editor "Html", Html
Set Editor = Nothing
%>
And in news.js I have:
CKEDITOR.addTemplates('news',
{
imagesPath: CKEDITOR.getUrl('/JavaScripts/ckeditor_3.6.3/templates/images/'),
templates:
[
{
title: 'News Template',
//image: 'template1.gif',
html:
'<h3>Template 2</h3>' +
'<p>Type your text here.</p>'
}
]
});
And it renders:
<script type="text/javascript" src="/ckeditor/ckeditor.js?t=C3HA5RM"></script>
<script type="text/javascript">//<![CDATA[
CKEDITOR.replace('Html', {"contentsCss": "\u002FStyles\u002FEditor.css","templates_files": "\u002Ftemplates\u002Fnews.js","width": 570});
//]]></script>
It seems like it takes the path provided (/templates)... and does a 404 with each letter from it.... i.e /t /e /m ...
What might be missing?
According to the documentation, the templates_files should be an array of strings, even if you use only one file, so the correct configuration is
Editor.Config("templates_files") = Array("/templates/news.js")

Resources