Ruby on Rails: Why doesn't my delete link work? - javascript

I am following the Rails guide: http://guides.rubyonrails.org/getting_started.html
and I have the following line in my index.html.erb:
<%= link_to 'Destroy', team_path(team), method: :delete, data: { confirm: 'Are you sure?' } %>
But it doesn't work; it just goes to the show page.
For your information, this is my routes.rb:
Rails.application.routes.draw do
get 'bets/index'
root 'bets#index'
resources :teams
end
My controller:
def destroy
#team = Team.find(params[:id])
#team.destroy
redirect_to teams_path
end
My application.js:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree
//= require jquery
//= require jquery_ujs .
My application.html.erb:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'defaults', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
Please note that I have tried changing <%= javascript_include_tag 'defaults', 'data-turbolinks-track' => true %> to <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>, but this gives me the following error:
Showing C:/Sites/BettingSite/app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method
(in C:/Sites/BettingSite/vendor/cache/ruby/2.1.0/gems/turbolinks-2.5.3/lib/assets/javascripts/turbolinks.js.coffee)
Line 6 is the line: <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Can someone please help me?

So it seems like the problem is that Windows is not working well with CoffeeScript. According to this question, add this gem to your gemfile:
gem 'coffee-script-source', '1.9.1'
That, or remove all CoffeeScript from your code by renaming your .coffee or .js.coffee files to regular .js files (and, of course, changing your CoffeeScript code to regular JavaScript). You would also have to remove any CoffeeScript gems from your gemfile.
BTW, when you update your gemfile, you must run bundle install and then restart your server.

Related

Rails 5, javascript is not loaded in production

In my app I use the bootstrap theme (gentelella), and in one of my pages I have a Moris graph which works perfect in Development evniroment but in production(heroku) doesn't work.
The error in the console is the following:
**Uncaught ReferenceError: Morris is not defined
**
It looks like the js for the Moris graph(the script is included in gentelella) is not loaded but everything else works fine.
Code for my graph:
<div id="graph_line"></div>
<script type="text/javascript">
new Morris.Line({
element: 'graph_line',
data: [
{Date: 'mydate1', y: myvalue1},
{Date: 'mydate2', y: myvalue2},
{Date: 'mydate3', y: myvalue3}
],
xkey: 'Date',
ykeys: ['y'],
labels: ['Y'],
hideHover: 'auto',
lineColors: ['#26B99A', '#34495E', '#ACADAC', '#3498DB'],
resize: true
});
</script>
I don't include any Moris.js because it is already included in my theme.
I don't understand why it works fine in local and not in production.
After some researching, I added this line in my production.rb:
config.assets.debug = true
and in heroku logs I have the error:
The asset "application.css" is not present in the asset pipeline
and the app is not running but it runs without the above line.
Any idea why my graph is not working in production and why heroku does not see my application.css
P.S in my assets I have application.scss and not application.css could this cause the problem? But I need the file to be .scss
My application.js:
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.remotipart
//= require pnotify
//= require unobtrusive_flash
//= require flashes
//= require gentelella
//= require gentelella-custom
//= require_tree .
The morris scripts are initialized in the gentellela-custom.js (init_morris_charts();)
And my application.html.erb
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
Update you code to have a debugger reference:
<%= debugger; %> before the line with: new Morris.Line({, and then open your favorite browser, switch to the debugger window and you will have a break point in there.
That will help you see if Morris is loaded at that point in time.

jquery not working in rails

I've read many pages about using jquery in rails and still can't seem to get it to work.
I have the 'jquery-rails' gem, and I installed.
I have the require statements in the application.js file.
Here is a test page I keep running:
<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title)%></title>
<%= javascript_include_tag 'application','jquery', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<script>$(document).ready(function(){
$(".bg-info").click(function(){
$(this).hide();
});
});</script>
</head>
<body>
<center>
<h1 class="bg-info"><%= yield(:title)%></h1>
</center>
</body>
</html>
But when I click on the "bg-info" text in the browser I get no response.
Here's what you should have:
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
$(document).on("click", ".bg-info", function(e){
$(this).hide();
});
Here's what I'd do for the layout (to test):
#app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title)%></title>
<%= javascript_include_tag 'application','jquery', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="bg-info"><%= yield(:title)%></div>
</body>
</html>
If the above doesn't work out the box, you'll probably have an issue with your development environment (OS) -- most likely with ExecJS.
To confirm this, you'll need to debug your developer's console:
To access the dev console, right-click > inspect element > console.
This shows you any errors with your JS & your front-end environment. If any errors appear, they may tell you what the issue is.
If no errors appear, you should try installing NodeJS on your system, as this ensures Rails can use the appropriate JS executable.

AssetFilteredError while working on rails with angular

Following this angular rails tutorial, getting the error of Asset Filtered error
application.html.erb file is
<!DOCTYPE html>
<html ng-app>
<head>
<title>Blog</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application", controller_name %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
main.js.coffee [created a subdirectory in app/asset/javascript/controllers/main]
#= require_self
#= require_tree ./controllers/main
application.js
//= require jquery
//= require jquery_ujs
//= require underscore
//= require angular
Its not loading the angular controller created in app/asset/javascript/controllers/main/mainIndexCtrl.js.coffee
#IndexCtrl = (#scope) ->
$scope.title = "My blog"
Showing the error as follows
Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( main.js ) to config/initializers/assets.rb and restart your server
But after changing like this its not showing $scope.title data.
Add Following code in config/initializer/asset.rb Rails.application.config.assets.precompile += %w( main.js )

Rambling-slider-rails shows only blank screen

I'm making an app using Rails 4.0.1 with Ruby 2.0 and I'm running into trouble installing the "Rambling slider" image carousel from here.
Here's the portion of the gemfile:
...
gem 'paperclip'
gem 'rambling-slider-rails', :git => 'https://github.com/gonzedge/rambling-slider-rails'
gem 'uglifier', '>= 1.3.0'
...
I ran bundle install and, following the instructions, put a reference to jquery.rambling.slider in my manifest files.
My app/assets/javascripts/application.js file:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require jquery.rambling.slider
//= require_tree .
My app/assets/stylesheets/application.css file:
/*
*= require jquery.rambling.slider
*= require_self
*= require_tree .
*/
I included the link tags in app/views/layouts/application.html.erb like so:
...
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag 'jquery.rambling.slider' %>
<%= javascript_include_tag 'jquery.rambling.slider' %>
<%= csrf_meta_tags %>
...
Then added some dummy images and the invoking javascript in the actual view accordingly:
<div id="slider">
<%= image_tag "lorem1.jpeg" %>
<%= image_tag "lorem2.jpeg" %>
<%= image_tag "lorem3.jpeg" %>
</div>
<script>
$(window).load(function(){
$('#slider').ramblingSlider();
});
</script>
But all that appears a simple 1'2'3 on the page.
I know that the images are referenced correctly because when I comment out the script in the view file, the 3 images appear with no problems.
The developer tools in Chrome only show that there is a single warning saying "event.returnValue is deprecated. Please use the standard event.preventDefault() instead." at jquery.js?body=1:5375. (I can show this line too if you need it) This warning only appears when the script is uncommented. As soon as I comment it out, the warning goes away.
Any help would be much appreciated. Thanks in advance!
The problem you mentioned(1,2,3 links) occurs because the theme effect was not included, You can include this by adding a div with "theme-default" class, present in the .css file of rambling-slider. For this to work you also need to add the themes folder in stylesheet & images folder of your application
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div class="ribbon"></div>
<div id="slider" class="ramblingSlider">
<%= image_tag "lorem1.jpeg" %>
<%= image_tag "lorem2.jpeg" %>
<%= image_tag "lorem3.jpeg" %>
</div>
</div>
</div>

Ruby on Rails 3 javascript_include_tag

When I have this as my javascript_include_tag in the head section application.html.erb:
<%= javascript_include_tag :defaults %>
I can successfully use disable_with on submit_tag, but when I change it to:
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"%>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" %>
<%= javascript_include_tag "application" %>
..in order to use a jQuery calendar I cannot use disable_with anymore. It doesn't work anymore.
Any ideas?
You're missing rails.js add that as well to your includes
<%= javascript_include_tag "rails" %>

Categories

Resources