I need to conditionally use the remotipart gem. The [docs][1] say just add it to application.js:
//= require jquery.remotipart
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
<%= javascript_include_tag "jquery.remotipart" %>
I get an error. How do i reference a js included as part of a gem generically, and remotipart js specifically?
Thanks,
Kevin
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
What means conditionally in this context? The most simplest way would be
<%= javascript_include_tag "jquery.remotipart" if condition %>
Also you could use content_for like this on the views where you want to include it:
# in your view
<% content_for :js do %>
<%= javascript_include_tag "jquery.remotipart" %>
<% end %>
# in your layout.html.erb
<%= yield :script %>
https://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
If you meant that you get an error that the JS file can't be found, please update your question with what library (webpacker, sprockets) and Rails version you use.
Related
How can I edit the following below in the index page? I would like to be able to inline edit and update the following the view has the index action
<% #request.each do |s| %>
<%= s.message %>
<%= s.date %>
<% end %>
Tried the gem best in place but i doesnt seem to work so whenever i tried
<%= best_in_place #request, :message %> it throws an error of unknown method :message. Isnt this <%= best_in_place #request, :message %> the same with this <%= s.message %>
Does best in place work in rails version 5.1.4 and how can I make the inline edit to work ?
Does best in place work in rails version 5.1.4 and how can I make the
inline edit to work ?
Yes, it works, you need jQuery and to add the gem and JS libraries.
For jQuery:
$ yarn add jquery
Then in your application.js file:
//= require jquery
For best_in_place, add the gem in the Gemfile:
gem 'best_in_place', '~> 3.0.1'
Then the library in your application.js file:
//= require jquery
//= require best_in_place
...
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
You see the best_in_place js right after jquery, and the initialization in the same file - you must add //= require_tree . for initializing in the same file.
Then in your view you need to pass the object and the attribute:
<% #request.each do |request| %>
<%= best_in_place request, :message %>
...
<% end %>
I am using Rails 4, Wicked_PDF and Chartkick Gem's
For Google Charts I use:
<%= javascript_include_tag
"//www.google.com/jsapi", "chartkik" %>
The html view comes up with charts and everything as expected.
When I append .pdf to the url the pdf document shows in the browser but the ChartKick charts do not show.
The following error appears where the chart should be:
Error Loading Chart: No adapter found
I have found the following online in the PDFKit documentation.
Resources aren't included in the PDF: Images, CSS, or JavaScript does
not seem to be downloading correctly in the PDF. This is due to the
fact that wkhtmltopdf does not know where to find those files. Make
sure you are using absolute paths (start with forward slash) to your
resources. If you are using PDFKit to generate PDFs from a raw HTML
source make sure you use complete paths (either file paths or urls
including the domain). In restrictive server environments the root_url
configuration may be what you are looking for change your asset host.
I am assuming that wkhtmltopdf is not finding the link to the charts, but I am not sure how to fix this.
Does anyone have a suggestion?
I found this link:
Render jQuery in wicked_pdf
Where Unixmonkey helps FattRyan to solve this for Highcharts.
Can anyone help how to set this wicked_pdf_javascript_include_tag so that Wicket_PDF will accept charts from Chartkick using Google charts?
You have to specify a protocol http or https when referencing to a CDN inside the pdf layout.
Also chartkick is served via the assets pipeline, so use wicked_pdf_javascript_include_tag instead.
Replace this line:
<%= javascript_include_tag "//www.google.com/jsapi", "chartkik" %>
With this:
<%= javascript_include_tag "https://www.google.com/jsapi" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
That's how I do it in a project of mine.
Cheers.
I struggled with this for a bit and the other answers were only partially useful for me. I wanted to provide more detail for anyone in the future:
The 4 major things I did to fix this for us were:
(1) Not using the middleware approach and instead using one off ruby embedded pdfs based off of a PDF layout you create
#Example layout file
#app/views/layout/pdf.pdf.rb
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%= wicked_pdf_stylesheet_link_tag "print" %> #print specific stylesheet
<%= yield :head %>
</head>
<body>
<h1>PDF Report</h1>
<%= yield %>
</body>
</html>
(2) using wicked_pdf's asset helpers to load ONLY the javascript we needed on those pages for assets we store in the app (all CDN based assets can be loaded with a normal javascript_include_tag)
#Example page template for a PDF you're downloading
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<p>All your pages content</p>
(3) using the javascript_delay option
#Use it universally from the initializer or on the specific PDF rendering in the controller
#config/initializers/wicked_pdf.rb
WickedPdf.config = {
javascript_delay: 3000,
other_options...
}
(4) pass the "discrete" axis option otherwise we only saw the axis and no data for time based charts (line/area/etc.)
#In the above template, wherever you render your chart
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<%= area_chart #data_retriever.time_based_data, discrete: true %>
<%= pie_chart #data_retriever.other_data %> # the default is discrete: false so no need for another option
Add the following to the top of the view your trying to convert to a pdf:
<%= wicked_pdf_javascript_include_tag "application", "chartkick" %>
I got it to work with Alex Villa's answer and from the answer to a similar question by installing the latest wkhtmltopdf version then specifying the javascript_delay option in the controller in step (3):
respond_to do |format|
format.html
format.pdf do
render pdf: "filename",
javascript_delay: 3000,
template: 'template_path.pdf.erb',
layout: 'pdf.html'
end
end
If someone gets the same issue with Rails 4 in 2019, then try to freeze chartkick version on 2.3.5. Because from the 3.0.0 version they removed support for Rails < 4.2. See
chartkick CHANGELOG
gem 'chartkick', '2.3.5'
Add this at the beginning of head in your pdf view file:
<%= javascript_include_tag "https://www.google.com/jsapi" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
And replace the wkhtmltopdf-binary gem with wkhtmltopdf-binary-edge. I used 0.12.4.0 version.
gem 'wkhtmltopdf-binary-edge', '0.12.4.0'
That's all I did and it worked.
I am writing a Rails 3.2.1 application and I have some javascript code I'd like to put in for a single action view. It simply calls a jquery plugin and starts a countdown, but I'd like to write it in coffee script and I feel like the asset pipeline is the correct tool to do this.
Also I need access to the variables passed by the controller such as #question. How would I do this? I have looked into the coffeebeans gem but that only works for :remote=>true forms and links.
Your problem can be solved in different ways.
Add the assets dynamically
Add to our application helper the following method:
module ApplicationHelper
def include_related_asset(asset)
# v-----{Change this}
if !YourApp::Application.assets.find_asset(asset).nil?
case asset.split('.')[-1]
when 'js'
javascript_include_tag asset
when 'css'
stylesheet_link_tag asset
end
end
end
end
Call the helper method in your layout-file:
<%= include_related_asset(params[:controller].to_param + '_' + params[:action].to_param . 'js') %>
Create specific assets for your controller actions. E. g. controller_action.js
Use yield
Add <%= yield :head%> to your layout head
Include your assets from your action views:
<% content_for :head do %>
<%= javascript_include_tag 'controller_action' %>
<% end %>
Please see the Rails guides for further information.
To passing controller data to your javascript, you could do:
<%= javascript_tag do %>
window.error_message = '<%= j error_message %>';
<% end %>
Please see the RailCast Episode #324 for further information.
If there a way to include the content of a javascript file into a Rails view?
I know about Ruby's File.read, but I'm searching for some helper already done.
<%= javascript_tag do %>
<%= render :file => '/path/to/rails/app/path/to/javascript.js' %>
<% end %>
So you can include any javascript code located on your machine. But to be honest there's more appropriate way to do that kind of things.
This is a really generic question, to which I haven't found a simple answer.
I'm dealing with messy legacy code that specifies style several times for the same classes/views. I have .sass files in app/styles, .css files in public/stylesheets and public/css
I don't understand which stylesheets includes which, or if they ever do.
How do you match stylesheets to a specific view in rails? How do you define hierarchy between style so ones can override others?
What's the rails default for matching styles to views? I don't see any stylesheet_link_tag used in the app
The best way would be to put a named yield in your application layout:
<%= yield :head %>
Then use a content for block in your view:
<% content_for :head do %>
<%= stylesheet_link_tag :my_css -%>
<% end %>