rails chartkick google charts load times extremely long - ruby-on-rails-4

I am trying to use chartkick to plot some charts with the google charts API. The problem is that the line to set everything up is taking 10-20 seconds to load. Here is the relevant line
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
Has anyone else had this problem? If so how do you fix it?

I don't know why this is, but I managed to get it to go fast by adding http: in the url
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>

I generally get around this by grabbing a local copy of the file and only using it in development mode.
<% if Rails.env == "development" %>
<%= #include the local file here %>
<% else %>
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
<% end %>
Generally seems ok in production mode.

Related

Rails 4, Carrierwave, jcrop, mobile to POST, not UPDATE

Hoping someone can help me here - can't seem to figure this out.
I'm using Carrierwave to upload photos, and jcrop to crop. On my desktop, it works perfectly fine. I upload/create the photo, then the user crops/update the photo to the preferred size.
The WEIRD thing that happens is that when I'm on MOBILE (just mobile, not desktop/tablet), the update form_for will call POST, instead of UPDATE. But again, on desktop, everything works perfectly. Can anyone provide any help on why this behavior is happening on mobile only!?!
My code:
show.html.erb
<div class="container large-margin-left large-margin-right">
<div class="jcrop"><%= image_tag #photo.photo.large, :id=>'cropbox', :class=>'img-responsive' %></div>
<br>
<%= form_for [#user, #photo] do |f| %>
<% for attribute in [:crop_x, :crop_y, :crop_h, :crop_w] %>
<%= f.text_field attribute, :id=> attribute %>
<% end %>
<%= f.submit "Crop", :class=>'large-font pink-bg white caps sm-space', data: { disable_with: "Cropping..." } %>
<% end %>
</div>
wow - hopefully this helps someone else. But, it turns out Android doesn't play well with Model validations. (i had photo validations of size and image type), and this consequently threw off jcrop.

Rails - loops and routes, and getting . instead of /

I have three controllers, books, users and contributions. In a view in the books controller I have the following snippet -
<%= link_to contrib.user.username, show_users_path(contrib.user_id) %>
Which returns /users.n rather than /users/n
<%= link_to contrib.user.username, "users/#{contrib.user_id}" %>
does the same thing.
I've read SO questions about this problem with nested resources, and with custom routes, but I've simply got my routes set up as resources, as follows -
devise_for :users, :controllers => { registrations: 'registrations' }
resources :blogs
resources :books
resources :users
resources :contributions
In users/index I have the snippet
<%= link_to user.username, "users/#{user.id}" %>
which works fine. What is going on?
.....
The problem was I'm using an each loop. The whole snippet goes
<% if controller.controller_name == "books" %>
<p><strong>by <%=link_to book.user.username, book.user %></strong></p>
<% book.contributions.each do |contrib| %>
<p><%= link_to contrib.user.username, user_path(contrib.user) %></p>
<%end%>
<% end %>
I changed the fourth line to this -
<p><%= link_to contrib.user.username, user_path(book.contrib.user) %></p>
Which works. Can anyone explain why?
It's because the default path for the show action is object_path(object_id) - there i no default route called show_object_path - I was confusing the path with action.

Rails 4 Index.html.erb loop

So I have the following index for my mailboxer conversations:
conversations/index.html.erb
<p><% #conversations.each do |conversation| %>
<% conversation.participants.each do |participant| %>
<% if participant != current_user %>
From: <%= participant.name %> <br />
<% end %>
<% end %>
Subject: <%= link_to conversation.subject %> <br />
Date: <%= conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %> <br />
<%= link_to "Move to Trash", {:controller => "conversations", :action => "trash", :id => conversation.id}, :title=> "Move to Trash", :method=>'post' %> <br/> </p>
<% end %>
I came around to this because first it was saying there was no local method for "participant" and then it was saying there was no local method for "conversation." So I just made an each.do loop for both of them.
This works... It lists the sender and subject and the option to move each message to a trash folder... but it starts looping about 5 times per second to infinity. (I'm assuming this is hell on the server.)
THEN I noticed that this also happens for the users index.html and edit.html
Figured it out. I'll try to explain the problem and the solution as in-depth as possible.
Load the index.html.erb of a short page (user edit, index... whatever.)
tail the production or development.log file (or just look at the webrick screen
Notice that it's looping over and over and your cpu is heating up.
Turns out it was some crazy endless_scroll.js that never did anything for anyone... that all-of-a-sudden decided to activate and loop any page it deemed was too short.
Now the crazy thing (and mind you, I'm typing to myself at this point) is that, if you just threw in some random paragraphs... it would stop. Delete them? It would start looping again. (I guess according to the scroll limit.)
So I just deleted the endless_scroll.js and did a rake assets:clean and a rake assests:precompile.
Now everything is back to normal.

Uploading a file without a form - Rails

Ruby 2.1
Rails 4.1
It should be pretty simple to upload a file without using any gems. I would include in a form something like this:
<% form_for #upload, :multipart => true do |f| %>
<%= f.file_field :uploaded_file %>
<%= f.submit "Upload file" %>
<% end %>
What I want to do is upload the file through a single menu action. How do I go about doing this?

Dropdown select list won't display items with custom forms of Zurb Foundation 4 and Rails 4

I've been having this annoying problem in which I click over a select dropdown input of my custom form, styled with Zurb Foundation 4 in my Rails application, and the list won't show its elements.
I thought at a start that was a problem with simple form, but I changed the f.association for f.collection_select, my code looks like this:
<h2><%= I18n.t(".sign_up") %></h2>
<%= simple_form_for(resource, :html => {:class => "custom"}, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<%= devise_error_messages! %>
<%= f.input :rut %>
<%= f.input :name %>
<%= f.input :email %>
<div>
<%= f.label :supplier_type_id %>
<%= f.collection_select :supplier_type_id, SupplierType.all, :id, :name %>
</div>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.error :base %>
<%= f.submit I18n.t(".sign_up"), :class => "button" %>
<% end %>
<%= render "devise/shared/links" %>
The most strange thing is that sometimes I'm able to see the items when refreshing the page, but when I get to the page navigating from other view of the app then it won't work. I've also noticed this when using hints for forms (i.e: If I have two hints in the same form, in different inputs, only one would show, but the same one message displays in both inputs when each message should show in their respective input. When reloading the page sometimes it shows one hint, and sometimes the other)
The styling looks good, so I think that it might be a foundation javascript problem.
Another thing I've noticed is that when I load the page the styling does a kind of "blinking" when using custom forms. This blinking it looks likes foundation takes a while to load the styling, I've also noticed this on their own custom form documentation site. This may indicate that is a form styled with javascript events or something similar, so this might mean that javascript is working well.
In addition, the checkboxes are having a similar problem, they only can be checked just when you reload the page, it might have some relation with this problem.
I'm very lost, some help would become very handy. Thanks!
--edit: Foundation 5 doesn't include custom forms and works better--
You might need to refresh dropdowns on each page:change event. Try something like this:
$(document).on("page:change", function() {
// SELECTOR_TO_CUSTOM_DROPDOWNS should select any Zurb custom dropdowns you
// are using.
$(SELECTOR_TO_CUSTOM_DROPDOWNS).trigger("change");
});
That's from the documentation on Zurb custom form JS.
Yes, this is caused by turbolinks. It stops $(document).ready from firing on page load, which is required by foundation's custom forms.
Using ssorallen's answer and to be more unobtrusive than the OPs, add this to application.js:
$(function(){ $(document).foundation(); });
$(document).on("page:change", function() {
if ($('form.custom').length > 0) {
$(document).foundation('forms');
}
});
Also, if you have jquery/coffeescript that relies on document ready being fired, add jquery turbolinks to your Gemfile.
Ok, so I just figured out that I have repeated asset load on my browser. This is causing a javascript error. It appears that deleting in the manifest
//= require turbolinks
Solves the problem.
I solved my multiple asset problem changing
<%= javascript_include_tag "application" %>
To the head in my application layout.
After watching the turbolinks railscast I noticed that besides my multiple asset loading, turbolinks and foundation 4 may not be compatible, it might be a solution on this post. But still doesn't work perfect for me.
I also noticed that navbar is also affected by turbolinks.
I think that this is rather a turbolinks problem and not an specific foundation dropdown. I will close this question and open a new on turbolinks and foundation.
Thanks to some Nick Reed insights I found out that the foundation gem was initializing foundation in application.js like this:
$(function(){ $(document).foundation(); });
So I checked the docs and I used this:
<script>
$(document).foundation();
</script>
After the "/body" tag in the application layout and everything seems to be working like a charm!