Sprockets cannot precompile chartkick - ruby-on-rails

I am using chartkick (added gem 'chartkick' in my Gemfile) and have defined it in my views/layouts/application.html.erb like this:
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
But on running the server, it results in an error:
Sprockets::Rails::Helper::AssetNotPrecompiled in Main#index
chartkick.js
I am using rails 5.0.7. I tried following the way mentioned in https://chartkick.com/#rails-5-sprockets. But still, it results in the same error.
Can you help me in finding what I'm doing wrong?
Or If anyone could tell me what direction should I head to, that would be awesome as well.
UPDATE: Adding config.assets.check_precompiled_asset = false fixes the issues.
But, adding this line means it will not verify/check precompiled_assets. Which is not a good thing, is it?

Related

Ruby on Rails, image does not load

I am attempting to do some basic Rails. I am trying to load an image from the app/assets/images folder using the following code:
<%= image_tag 'image.jpg'%>
It generates the following address (I am using localhost:3000):
http://localhost:3000/images/image.jpg
I have attempted all of the different iterations of Rails images I could find including using <img> tag with src="/assets/images/image.jpg" etc. All with no luck.
Also attempted changing production.rb (edit: and development.rb) to have the following code:
config.serve_static_assets = true
config.assets.compile = true
Following the localhost, link gives an error
"No route matches [GET] "/images/image.jpg""
The public path that Sprockets uses by default is /assets.
This can be changed to something else:
config.assets.prefix = "/some_other_path"
from rails guides
So your image must be available at http://localhost:3000/assets/image.jpg
PS: Just as an advice, try to read the guides, that will definitely help you out in future.
Try to remove
config.serve_static_assets = true
config.assets.compile = true
and add
config.assets.debug = true
config.assets.quiet = true
to your development.rb
I figured it out:
To solve a separate issue where I was getting an error (TypeError: Object doesn't support this property or method) on
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
I changed the 'application' to 'default' as per someone's suggestion. It fixed the issue but it seems this change was later causing my image problems. To fix the image problems I changed it back to 'application' and then to fix this original TypeError I removed the require_tree from application.js and it worked. Here's to hoping that doesn't break something else.

Rails "Browser Gem" support

I'm new to Rails, I'm trying to get the "browser" gem working.
(https://github.com/fnando/browser)
I was hoping someone had some example code they code post. I installed the gem by putting gem "browser", require: "browser/browser" in my gemfile.
I ran gem install browser
So I don't really know how to use the gem to actually get the users browser info. I tried just doing this as a test in the view <%= browser.full_version %> but I get Template::Error (undefined local variable or method browser'
any example code on what to put in a view/modal/controller would be appreciated, thanks.
You need to instantiate the object. So you can do something like this:
def index
#browser = Browser.new(request.env["HTTP_USER_AGENT"])
end
Then you can call the object in your view:
Device: <%= #browser.device.name %><br>
Platform: <%= #browser.platform.name %><br>
Info: <%= #browser.to_s %><br>
PS. I tried using this gem but found useragent which seemed to better display the information I wanted.
You can simply install the gem and "call it" in your view.
For example:
<% if browser.device.mobile? || browser.platform.android? %>
To install this gem you need:
add gem declaration to Gemfile
run bundle install from the console
restart your development server
P.S. when you are starting to use any gem, you should carefully read gem's README first. In 95% cases they are documented good enough.

Capable of hosting filter related icons in Wice grid error

I am trying to find a resource in solving this issue. I am using the wice grid gem for my project and I encounter this error undefined method capable_of_hosting_filter_related_icons?' for nil:NilClass. Below is my view:
-grid(#vendors_grid) do |g|
g.column name: 'ID', attribute: 'ID'do |vendor|
vendor
[...]
link_to('Edit', vendor_index_path(vendor))
I converted the erb example from the readme file to haml. I followed the install instructions carefully to ensure I do not miss anything.
Make this small correction -grid(#vendors_grid).each do |g| and try again. It could be that you are not enumerating over an array, in which case you are not getting back the right object

form_for wrong number of arguments in rails 4

I am using form_for tag and Its working in Rails 3.0.4 environment.
But when I tried to update my project to Rails 4.It gives following errors
wrong number of arguments (3 for 2). Here is my code
<%= form_for #email, :url => alerts_path do |f| %>
<% end %>
Try to remove things that may try to change things in views.
In my case the problem was with client_side_validations gem
Hope this helps.
Not sure why yet, but removing the "meta_search" gem from my Gemfile fixed this exact problem for me on Rails 4.
If you google this problem you may find this GitHub issue that attributes it to the "client_side_validations" gem, but using the suggested "rails-4-quick-fixes" branch of that repo didn't fix the issue for me.
I think that the error with simple_form_for was a red herring, as I was getting the same error even using Rails built in form_for, and was getting wrong number of arguments (3 for 2) regardless of how many arguments I actually passed into either method.
meta_search hasn't had a new release since February 2, 2012, so I can only assume something in the gem wasn't behaving nicely with Rails 4. If I discover what the problem was specifically, I'll update this answer. It seems kind of lame to have to "just stop using" meta_search, but that's as much as I can offer so far. Thanks!
I can't see any issue with your form_for, can you post backtrace of exception ? may be another gem is overriding default form_for ?
Your syntax is correct.
Try changing it like this:
<%= form_for(#email, url: alerts_path) do |f| %>
#form fields goes here
<% end %>
I tried to see if there is any change in source-code for form_for in Rails 4.0 API; It's still the same.
Have a look at Line 262 at https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb
Let me know if it works
Remove gem 'meta_search' from your gem file, and look into using gem ransack instead. https://github.com/activerecord-hackery/ransack

Issue with Javascript include in Rails app

I'm using Rails 3.2.8. When the app is deployed access the view that is including a javascript:
<%= javascript_include_tag "epiceditor" %>
Heroku fails with this log:
ActionView::Template::Error (/app/app/assets/javascripts/epiceditor.js.erb has already been required
I've checked some possible solutions, like checking for any reference that may trigger a circular dependency, or simply removing it in case it is being included somewhere else, which isn't. So, if I include it, I get this "has already been included error", if I don't , then the file isn't included at all.
My config/application.rg has this
config.assets.initialize_on_precompile = false
And applications.js has this:
//= require jquery
//= require jquery_ujs
//= require tabs
It might be important to note that the file the tag is referencing is "epiceditor.js.erb", since it has some embedded Rails code that I needed.
Thanks for your help
EDIT:
I believe this is a bug in Sprockets. If I update Rails to 3.2.9rc2, the error is now this:
ActionView::Template::Error (Asset logical path has no extension: epiceditor/.js
but of course the extension in epiceditor is epiceditor.js.erb, and I've tried being explicit about it in the javascript_include_tag as well.
I found the bug.
It turns out that inside the .js.erb file I'm calling
<% asset_path 'epiceditor/' %>
which should expand to the path where all the epiceditor file are placed, but instead is actually loading the file itself in recursive manner. This is expanding properly in the development environment but not in the production environment. Funny, right?
The reason for this is that is adding a digest. So I fixed the whole issue with this:
<%= asset_path 'epiceditor/', :digest => false %>
and now it does expand to the directory, and doesn't fall into the recursion trap.
Hope this saves some time for someone!

Resources