Unable to delete and display confirm box - javascript

I am unable to delete or display a confirm box in Rails 3.1.
I have the following lines in my application.hmtl.erb layout :
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
I have also tried to change "application" to :defaults or "prototype" or "jquery" but it didn't change anything.
Need help with this !

Confirm boxes and routing to REST actions are done by the the unobtrusive javascript file. You have this problem, because you don't have it included.
Check inside your application.js if you have the following lines (before any actual javascript code):
//= require jquery
//= require jquery_ujs
This example assumes that you have the jquery-rails gem in your Gemfile. If you have prototype-rails, you should switch to:
//= require prototype
//= require prototype_ujs

Related

Rails 5 - Rails-ujs has already been loaded

I have put the tag javascript into my body application.
In my console I am getting this error:
Rails-ujs has already been loaded.
In development mode the app works, however it doesn't work in production.
I have deployed my rails application on Heroku.
I try to move the tag javascript into the head, but then all function js is not working.
Any solution?
If you're using webpack and a separate front-end, you might have two instances of your javascript tag in application.html.
= javascript_include_tag 'application', 'data-turbolinks-eval' => false
= javascript_pack_tag 'application'
You'll want to remove the first instance; similarly if you've used pack_tag to render your stylesheets, you may be experiencing CSS classes being called twice (you'll see stacked classes on an element in dev tools).
I solved bringing my javascript into the head and simply including all my functions below:
$ (document).on('turbolinks: load', function () {
})
If you do not use webpack then use as like:
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload', 'data-turbolinks-eval': false %>
<%= javascript_include_tag 'frontend_application', 'data-turbolinks-track': 'reload' %>
</body>
in application.js file
//= require rails-ujs
//= require activestorage
//= require turbolinks
in frontend_application.js file
//= require compress.min
//= require custom

Rails_Javascript loads only if I pass by index home page

I am facing a javascript issue on Rails;
I used this web app in order to publish a book rating system
I've noticed that when I go to my homepage (index), then I click on the book that I want to rate (, the script loads and everything works fine, but when I go directly to the book without passing by the home page, the script of rating will not load
on application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .
on the head of application.html.erb
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
What am I missing ?
It may be caused by turbolinks which uses AJAX to view templates to speed things up. But it will cause JavaScripts to not load without a page refresh. You can try removing the turbolinks gem from your gemfile and run bundle install. Alternatively you can wrap the raty javascript with the following. So put the first line at the beginning of your javascript and close your javascript with the last line.
$(document).on('turbolinks:load', function() {
...your jQuery/javascript goes here...
});

Rails Javascript onchange works with <script> but not externally

In my index.html.erb file I have a form that successfully submits onChange. However, this only works if I use <script> tags placed after my form. Why is the same js in welcome.js not running? Index loads as a /welcome route.
# app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ace.min
//= require jquery.sortable
//= require chartkick
//= require hammer
//= require_tree .
External js file
# app/assets/javascripts/welcome.js
$('.chart-radio-icon').change(function(){
$('#mega_form').submit();
});
View
# views/welcome/index.html.erb
<%= form_tag mega_chart_path, id: 'mega_form', remote: true do %>
<%= radio_button_tag :metric_type, :followers_count, true, :class => 'chart-radio-icon'%>
<%= label_tag(:followers, 'Followers', :class => 'chart-label chart-label-social') %>
Again, the js works when placed in <script> tags in the view, but not externally.
EDIT:
Since we've determined that your asset files are not being included in development. For a sanity check try this and restart your server:
Rails.application.config.assets.precompile = %w( application.js application.css )
END EDIT
Since you are using a require_tree ., and since the welcome.js file is in app/assets/javascripts, it will be included on all pages in your application. See here.
I think that the problem is that on page load, the event listener is not being assigned to the DOM element. Try wrapping your JS code in:
#app/assets/javascripts/welcome.js
$(document).ready(function(){
....
});

DEVISE - No route matches [GET] "/users/sign_out" - Rails 4

Nothing on the internet solved this problem.
I'm running on Rails 4.2.3 and Ruby 2.1.5
After an installation of devise, I got this error.
When I delete the line
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true % from my layout file, the error is gone and everyting looks fine.
I tried everything found on stackoverflow, didn't work.
Right now:
I use <%= link_to "sign out", destroy_user_session_path, method: :delete %> in my layout.
I tried to change the initializer : config.sign_out_via = :delete to "get" also, didn't work. Now remains "delete". Anyway it should be delete in my opinion.
My application.js file includes : //= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Routes.rb file includes devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}. If I don't add this line, it's not working also.
If I delete <%= javascript_include_tag 'application', 'data-turbolinks-track' => true % from my layout file it's working. If I don't, it's not working.
What is the problem with this javascript thing? Any opinion is welcome.
Try using the jquery-turbolinks gem and modify your application.js to look like this:
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require_tree .
//= require turbolinks
Ok the problem seems like because of Windows. The solution is adding the gemfile : 'coffee-script-source', '1.8.0' and bundle update coffee-script-source This solved.

Add javascript to Foundation

I have installed the Foundation 4 gem but now I would like to add some javascript such as dropdown buttons. I followed the instructions but zepto, jquery and foundation.min.js are not loaded when I get a view ("No route matches")
I added in the application layout :
<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application" %>
<script src="/js/custom.modernizr.js"></script>
<%= csrf_meta_tags %>
in the head
<%= javascript_include_tag "js/vendor/custom.modernizr" %>
<script>
document.write('<script src=/js/vendor/'
+ ('__proto__' in {} ? 'zepto' : 'jquery')
+ '.js><\/script>');
</script>
<script src="js/vendor/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
at the end of the body.
But there is no folder js/vendor right ? Where must I put them ?
Thank you in advance !
If you place JavaScript in your vendor folder, you can include it in your Rails application by adding it to your application.jsfile.
sample application.js
//= require jquery
//= require jquery_ujs
//= custom.modernizr
//= require foundation
//= require_tree .
Sprockets will look in your various assets directories to find your JavaScript.

Categories

Resources