cloudinary-direct upload from browser not uploading - ruby-on-rails

When I try to upload a file directly to Cloudinary nothing happens. The file name in the selected file field is deleted and no file is added to my Cloudinary accounts' Media Library.
This is my index.html.erb file
<%= cloudinary_js_config %>
<%= form_tag(cardset_cards_path(#cardset, #card), :method => :post) do %>
<%= cl_image_upload_tag(:image_id) %>
<%= submit_tag 'Submit' %>
<% end %>
My application.js file
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require cloudinary
I do have the downloaded cloudinary.yml file with api_key and api_secret...
I'm using Rails 4, I haven't set my config.assets.enabled = false, so I belive I am using Asset Pipeline by default.
I believe I'm following the example in Cloudinary, but my Cloudinary Account Media Library doesn't show any file uploaded. I've tried adding :html => {:multipart => true}, but the same things happens nothing. I've tried different things in the controller to get the returned id, but since nothing is happening, I think I'd just be happy to get a file into my Cloudinary Media Library.
-hoping to rise from the dirt to the Clouds

Related

401 Unauthorized error when uploading an image to Cloudinary

I'm using ruby (2.0.0), rails (3.2.17), cloudinary (1.0.70), and jquery-rails (3.1.0). I'm getting this error only when the image upload tag is nested multiple levels deep. If it's only one level deep, I'm able to upload properly. The file upload that is generated is (cloudname, signature, and api key replaced with "xxxx" for StackOverflow):
<input
type="file"
name="file"
data-url="https://api.cloudinary.com/v1_1/xxxxxxxxx/auto/upload"
data-form-data="{"timestamp":1399504570,"callback":"http://localhost:8080/cloudinary_cors.html","signature":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","api_key":"xxxxxxxxxxxxxxx"}"
data-cloudinary-field="page[figures_attributes][1399504574259][images_attributes][0][asset]"
class="cloudinary-fileupload
">
Which, from what I have read seems to be correct, however I am still getting {"error":{"message":"Invalid cloud_name undefined"}} when I try a direct upload.
EDIT:
The URL that it hits when attempting to upload is https://api.cloudinary.com/v1_1/undefined/upload. This makes me wonder why the URL from the data-url attribute in the input is not being used and how this URL is generated.
I'm generating the file upload tag like this:
= f.cl_image_upload :asset
My application.js looks like this:
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require ckeditor/init
//= require cloudinary
//= require_tree ../../../vendor/assets/javascripts/.
//= require_tree .
Please let me know if you need any other information. Thanks!
The problem was that I wasn't including the cloudinary_js_config script anywhere. So I just added it to the top of the form like so:
= form_for #page, :html => { :multipart => true } do |f|
= cloudinary_js_config

Exporting a chart as image using lazy-highcharts gem

I am able to generate the charts using lazy highcharts but i want those charts to exported as images , here is my code which I am using to generate the charts
#fields= ReportHistory.all_history
#h = LazyHighCharts::HighChart.new('graph') do |f|
f.chart(:renderTo => 'container', :zoomType => 'x',:spacingRight=> 20)
f.title(:text => 'Reports')
f.xAxis(:title=>{:text => 'Days'}, :categories =>#fields.map{|x|x.Date}.last(limit=15))
f.yAxis(:title=>{:text=> 'Jobs_count', :type =>'integer' ,:max => 5000000})
f.series(:name =>'jobs_count', :data=> #fields.map{|x| x.jobs_count.to_i }.last(limit=15))
f.export(:type=> 'image/jpeg')
end
and in my view I to show the chart I have this
<%= high_chart("my_id", #h) %>
I want to have the effect like here where I can download the chart image using the export button.
First, you need to ensure that the export module is available and loaded by your view:
<%= javascript_include_tag "vendor/highcharts/highcharts.js" %>
<%= javascript_include_tag "vendor/highcharts/modules/exporting.js" %>
Second... there's nothing to do. As soon as the exporting modul is loaded, it will add the menu to export your graph.
Notes:
You may need to adapt the paths depending on your setup.
The exporting.js is part of the package you download from Highcharts.
You can add/update the following in your application.js
//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require highcharts/modules/no-data-to-display
//= require highcharts/modules/exporting
If you want also updated vendor assets of highcharts, you can run:
rake highcharts:update

gmap4rails in ActiveAdmin: maps does not appear

In try to use gmaps4rails in my ActiveAdmin / Rails app. To do so, I have added the following in my Gemfile:
gem 'gmaps4rails'
and ran a "bundle". I updated the 'show' method in app/admin/device.rb file with:
show do
attributes_table do
row :name
end
# Get device location
#markers = Location.all.to_gmaps4rails
div do
render "map"
end
end
In the app/views/admin/devices/_map.html.erb I have the following code:
<%= stylesheet_link_tag 'gmaps4rails' %>
<%= gmaps4rails(#markers) %>
<%= yield :scripts %>
In app/assets/javascripts/application.js:
//= require gmaps4rails/gmaps4rails.googlemaps
//= require gmaps4rails/gmaps4rails.base
//= require jquery
//= require jquery_ujs
//= require_tree .
And in my app/models/location.rb:
class Location < ActiveRecord::Base
acts_as_gmappable
attr_accessible :latitude, :longitude
def gmaps4rails_address
"#{self.latitude}, #{self.longitude}"
end
def location
[:latitude, :longitude]
end
end
When I go on the show page of a device, the map does not shows up (all blank). Is there any configuration I missed ?
UPDATE
I have checked with chrome developer tools and noticed the following error:
Uncaught SyntaxError: Unexpected token ;
that make reference to the line:
Gmaps.map.markers = ;
Fix with the following:
div do
markers = Location.all.to_gmaps4rails
render "map", { :markers => markers }
end
and :
<%= stylesheet_link_tag 'gmaps4rails' %>
<%= gmaps({
"map_options" => { "zoom" => 2, "auto_adjust" => false},
"markers" => { "data" => markers }
})
%>
<%= yield :scripts %>
You have to load the Javascript files in the /config/initializers/active_admin.rb; search for the section:
# To load a javascript file:
# config.register_javascript 'my_javascript.js'
Also, I think you should add the CSS files (if is not appearing) into the same file.
Remember that ActiveAdmin manages its own Javascript file, named active_admin.js; you can try loading the Javascript files in this JS; some time ago I had a similar problem and I only was able to solve it adding the files in the initializer.

Autocomplete Rails it is not working

What did I do wrong in this tutorial: https://github.com/grosser/simple_auto_complete
Here are my scripts by the way:
First I put this in my users_controller.rb
autocomplete_for :user, :username, :limit => 15, :order => 'created_at DESC'
In my routes.rb
namespace :profile do
resources :users, :only => [:index] do
collection do
post "search"
get "autocomplete_for_user_name"
end
end
In my application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.autocomplete.js
//= require jquery.js
jQuery(function($){//on document ready
//autocomplete
$('input.autocomplete').each(function(){
var input = $(this);
input.autocomplete(input.attr('data-autocomplete-url'),{
matchContains:1,//also match inside of strings when caching
// mustMatch:1,//allow only values from the list
// selectFirst:1,//select the first item on tab/enter
removeInitialValue:0//when first applying $.autocomplete
});
});
});
In my views (app/views/profile/messages/compose.html.haml)
%div#page-info
%span#title
Compose
%span#desc
Compose a new Message
= form_for :message, :url => send_message_profile_messages_path do |message|
%label{:for => "friend"} To:
%br
=message.text_field :auto_user_name , :class => 'autocomplete', 'data-autocomplete-url'=> autocomplete_for_user_name_profile_users_path
%script{:type => "text/javascript"}jQuery(function($){//on document ready $('input.autocomplete').each(function(){var $input = $(this);$input.autocomplete($input.attr('data-autocomplete-url'));});});
//= collection_select(:message, :friend_id, #friends, :id, :username)
%br
%label{:for => "message"} Body:
%br
= message.text_area :message
%br
= message.file_field :attachment
%br
= submit_tag "Send", :confirm => "Are you sure?"
I am trying to autocomplete the users using their username (s)
Note: I had also installed the gem and I think there's no problem about that.
What am i doing wrong?
In view
remove the comment //on document ready from script, as it is all in single line I think it is commenting the whole line including js code.
or use alternate formatting
:javascript
//on document ready
jQuery(function($){
$('input.autocomplete').each(function(){
var $input = $(this);
$input.autocomplete($input.attr('data-autocomplete-url'));
});
});
add //= require_self to application file for adding its own js code.
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.autocomplete.js
//= require jquery.js
jQuery(function($){//on document ready
//autocomplete
$('input.autocomplete').each(function(){
var input = $(this);
input.autocomplete(input.attr('data-autocomplete-url'),{
matchContains:1,//also match inside of strings when caching
// mustMatch:1,//allow only values from the list
// selectFirst:1,//select the first item on tab/enter
removeInitialValue:0//when first applying $.autocomplete
});
});
});
Also try not to have white spaces between require.
rails warning in application.js says
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
I have been using this autocomplete it works great for me
https://github.com/crowdint/rails3-jquery-autocomplete

Devise: Why doesn't my logout link work?

the problem: In a nutshell, when I try to install a logout link to my app it fails to work. Here's as much context as I can think to put here (if you want anything else, please poke me)...
I've got this in a haml view:
= link_to("Logout", destroy_user_session_path, :method => :delete)
It generates this in the view:
Logout
I verified that in my config/initializers/devise.rb I have this setting uncommented and correct:
config.sign_out_via = :delete
I validated the following route:
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
I also have this bit of trickery in my routes.rb, and I suspect this is related to my issue:
devise_for :users, :controllers => {:sessions => "devise/sessions", :registrations => "users"}
resources :users
This last bit is because I want to manage (edit, create and delete) users in my own controller.
The error message I'm getting is as follows:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=sign_out
Rails.root: /home/jaydel/projects/mbsquared-projects/Wilson-Goldrick
app/controllers/users_controller.rb:16:in `show'
In my server logs I see this for the request:
Started GET "/users/sign_out" for 127.0.0.1 at 2011-08-04 13:08:51 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"sign_out"}
Anyone have any ideas?
The correct way to fix this, REST-wise, would be to change your logout links to use the DELETE method. It's a very easy fix, changing this:
link_to "Log out", destroy_user_session_path
to this:
link_to "Log out", destroy_user_session_path, :method => :delete
I had the same problem with rails 3.2 when I deleted from application.js this line:
//= require jquery_ujs
So, I think you have to insert this line in your application.js if you haven't it there.
PS. This behavior means that rails adapter for jquery doesn't function. So you should make sure if it is loaded in your html in browser. You should test it in development mode because you will have compressed js in production and it will be very difficult to find something there.
The problem lies in the fact that in your logs the signout request is a GET request.
Started GET "/users/sign_out"
But the signout route is a DELETE
destroy_user_session DELETE /users/sign_out(.:format)
The reason why you are getting the exception is that is it getting confused with one of the routes created by resources :users which would be something like
edit_user GET /users/(:id)(.:format) {:action=>"edit", :controller=>"users"}
Basically 'sign_out' is being mistaken as a id.
I'm not sure why the delete link is not going through as a DELETE request. Though changing
config.sign_out_via = :delete
to be :get might solve the problem.
You need both
link_to "Log out", destroy_user_session_path, method: :delete
&& these included in application.js
//= require jquery
//= require jquery_ujs
. Obviously, don't forget to add this to Gemfile if for some reason it is not there.
gem 'jquery-rails'
I've tried all the solutions mentioned here but the true solution to this issue is the following :
<%= link_to "Sign Out", destroy_user_session_path, 'data-turbo-method': :delete%>
It works if it doesn't just refresh or restart the server.
This is an old question but the accepted answer is not the proper way to solve this issue (although it is a good diagnosis of the problem).
When I ran into this today my routes file appeared as:
root :to => 'welcome#index'
resources :users
devise_for :users
As mentioned by Olives the resources statement is simply causing /users/anything to trip up. I simply needed to reverse the ordering of my routes...
root :to => 'welcome#index'
devise_for :users
resources :users
jQuery is required in order to perform the request with DELETE. Make sure you didn't drop it from your application.js.
Since the OP mentioned devise and this problem also happened to me, I just wanted to chime in. #Ross Allen had the correct answer when you don't want to use jQuery_ujs. Rails provides a couple of helpers to solve this without jQuery.
This is Ross's answer but using Devise's current_user model:
<%= form_for(current_user,
url: destroy_user_session_path, html: { method: :delete }) do |f| %>
<%= f.submit "Log out" %>
<% end %>
Or the more succinct button_to helper that creates a similar form:
<%= button_to "Log out", destroy_user_session_path, method: :delete %>
This is an old question, but I wanted a way that required no JavaScript. Rails enables "DELETE" like requests with a hidden input that sets method to delete. Use a real <form>, and let Rails generate the necessary hidden inputs:
<%= form_for(resource, as: resource_name,
url: destroy_user_session_path, html: { method: :delete }) do |f| %>
<%= f.submit "Log out" %>
<% end %>
the above generates something like:
<form accept-charset="UTF-8" action="/users/sign_out" method="post">
<div style="display:none">
<input name="utf8" type="hidden" value="✓">
<input name="_method" type="hidden" value="delete">
<input name="authenticity_token" type="hidden" value="1234">
</div>
<input name="commit" type="submit" value="Log out">
</form>
I've checked all this solutions, but no one has worked for me. Finally I found that in header I deleted two lines including application tags and csrf meta
So, If someone have this problem with no sollution at this point - check if you have in header of the page (HAML notation)
= javascript_include_tag 'application'
= csrf_meta_tags
I had to go about this in a different manner as my application.rb had:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
and was still failing. It turns out I needed to set config.serve_static_assets = true in /environment/development, test and production.rb. This link was what helped me figure it out.
You also have to check, is resources :users under devise_for :users and is the link method delete
link_to "Logout", destroy_user_session_path, method: :delete
if both are right, your logout button should work
I agree that it's more restful to use the http DELETE method, I like to create a simple endpoint to destroy a user session with a GET method.
by adding this to config/routes.rb
devise_scope :user do
get '/logout', :to => 'sessions#destroy'
end
If your are also having more than one jquery sources in your rails app you need to use the default one specified in the Gemfile
make sure in your Gemfile you have this
gem 'jquery-rails'
and in your assets you do not have any other JQuery source files.
in my case I had a second JQuery file other than the default as declared in the Gemfile so it caused conflicts.
if you previously had those source, be sure to precompile assets again if you had previously done so
If you're finding this problem in production and you're using the asset pipeline, you often need to:
rake assets:clobber in order to clear out your public assets. Then rake assets:precompile before committing and pushing your code (unless your push also precompiles, aka Heroku).
the reason may be in the wrong order connection bootstrap-sprockets
the correct order:
// app/assets/javascripts/application.js
...
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
As mentioned by Tombart above, the answer for me was putting back the rails-ujs call in my application.js file.
//= require rails-ujs
//= require turbolinks
I had initially removed them to stop errors from having my scripts in the footer be re-evaluated. My workaround was to create a footer.js compile file with the resources I wanted in the footer.
//= require jquery-3.2.1.min
//= require jquery-ui-1.12.1.min
//= require materialize
//= require init
Then in my /initializers/assets.rb file adding a precompile
Rails.application.config.assets.precompile += %w( footer.js )
Then you can call the following in your layout/application.html.erb file.
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'footer' %>
I keep the application line in my header, and added my new "footer" call before the end of my body tag.
1) I feel u may need to install below gem, the request with method: :delete will work else it may go as get
https://github.com/rails/jquery-rails
2) Add this two below lines in application.js
//= require jquery
//= require jquery_ujs
Stop the server
yarn add "#rails/ujs"
yarn add turbolinks
yarn add #rails/activestorage
My application.js
import Rails from "#rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "#rails/activestorage"
import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
Start the server

Resources