Error in generated files of Ruby on Rails - ruby-on-rails

First of all, I open the Windows command prompt with Ruby and Rails via the shortcut provided by RailsInstaller. First of all, I create a new Ruby on Rails application using the following command. I call it "curso"
rails new curso
After that, I navigate to the project folder C:\Sites\pruebaand start the WEBrick server, like this:
rails s
I go to localhost:3000and the test page runs perfectly.
After that, I import the project in NetBeans. Then, I open the command prompt and create a new controller, like this:
rails g controller miprueba index
I check the file Views/layouts/application.html.erb, which was generated by Rails and it looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Prueba</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
NetBeans shows me an error on line 7. It says Unexpected ':'. The same error happens in line 8.
If I try running the application, in localhost:3000/miprueba/index, I get an error page titled "Action Controller: Exception caught". It says this:
ExecJS::ProgramError in Miprueba#index
Showing C:/Sites/prueba/app/views/layouts/application.html.erb where line #7 raised:
TypeError: El objeto no acepta esta propiedad o método
The last line roughly means "The object doesn't accept this value or method".
If I delete lines 7 and 8 the application runs just fine, but I'm curious about why are those two lines generated and why do they fail.

The first error being thrown by NetBeans is likely a red herring. The error concerning the unexpected ':' is related to the use of colon-hash syntax on an escaped symbol in an implicit hash. ERB isn't technically ruby, so NetBeans is probably getting confused.
The real error is related to running ruby on rails on Windows. The error is actually being thrown by a javascript runtime, not rails. The short answer is that you can install NodeJS to replace your existing runtime with one that works well with Rails. Or you can follow the instructions here:
execjsruntimeerror-on-windows
Rails on Windows is tough. Good luck!

Related

Rails - Linking stylesheet and javascript in layout causes syntax error - "Missing ///"

I came into work today to find that my project is throwing a syntax error for some reason, and I can't find a single thing about this error.
When using the stylesheet_link_tag or javascript_include_tag helpers, I receive SyntaxError: [stdin]:1:1: missing ///.
Some things I have noted are:
If the links dont lead to a valid file, it compiles just fine.
Even if the files are empty the error is still thrown so the syntax error is definitely in the erb, even though I havent changed my application.html.erb since adding the link for the fonts.
EDIT:
After rewriting the layout in haml, the error persists, so I suspect it is not in the erb but in the ruby functions for linking, although I don't know what could have changed.
I just noticed the "ExecJs::RuntimeError", which is interesting because I commented out the javascript link so i could deal with one at a time.
Any help is appreciated. I feel like I'm missing somethinng very simple here, but i cant find it for the life of me.
If you want to see any of my code let me know, I wasnt sure what would be relevant other than this error page:
EDIT 2:
Here's my "appllication.html.erb":
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>{Site Name}</title>
<%= csrf_meta_tags %>
<link href="https://fonts.googleapis.com/css?family=Lato:400,300|Raleway|Tangerine:700" rel="stylesheet">
<%= stylesheet_link_tag "application.css", media: "all", "data-turbolinks-track": "reload" %>
<%#<%= javascript_include_tag "application", 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
This error was caused by a compilation error somewhere in the rails asset pipeline. This specific instance was caused by a rogue TypeScript file in app/assets/javascripts after I had removed typescript-rails from my Gemfile.
Thanks to #dstarh I was able to find the error.
I'll leave this here since I couldn't find any information on this error anywhere. Hopefully it can help someone else.

Resource created as many times as accumulated requests

I have a really weird bug. I have a very simple Rails 4.2 app, and I was just starting to add some JavaScript functionality. I've started by adding a remote: true option to a form, but after that more than one resource are created in the database. It seems to have a criteria, though, and it is that the number of resources created equals the total amount of pages visited since the server started. So:
rails s and refresh the /new page, will create 1 resource.
Now visit page 1, page 2, page 3, and go back to /new and submit, and 4 resources are created.
Now visit page 5, page 6, go back to /new and submit and 7 resources are created.
As I have mentioned, I was just starting with JS, and only had another script that is not related with this in anyway (bug still happens if I remove this file and restart the server).
Just for reference, I've created a new Rails app and put remote: true to the form and, obviously, this is not happening, so something there is messing up my app.
I don't even know how to research this. Any clue on where should I start looking at?
EDIT: I've tried to reproduce the same situation in tests, using Capybara and doing several page visits before filling up a form, and I couldn't.
application.js
//= require jquery2
//= require jquery_ujs
//= require turbolinks
//= require_tree .
layouts/application.html.erb
Moved it to the end of file and added cache: true
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true, cache: true %>
</body>
</html>
Again: there was only one script in the app, and I've removed it. There are no more JS thing anywhere. None, at all.
I think the relevant question here is: how on earth can the number of page visits since the server was started be related to the number of resources created?
Thanks!
I can't yet believe it. It all was because of this:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true, cache: true %>
</body>
</html>
I had to place the javascript tag back to its original place, within the <head> tag.
<head>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true, cache: true %>
</head>
Don't ask me why, but placing the javascript file at the bottom of the file caused all this mess.
EDIT:
Turns out this is something related with Turbolinks. Removing Turbolinks fixes the issue, and I can load all JS at the end of the document, again. See this issue on Turbolinks, and also this on someone's project.

Rails Chartkick Gem Error Loading Chart No Adapter Found

I have a Rails 3.2.14 app where I'm using the chartkick and groupdate gem to try to generate some basic charts.
When I load my view I get the error:
Error Loading Chart: No adapter found
Here's what my view code looks like:
index.html.erb
<%= line_chart Call.group_by_week(:created_at).count %>
Here's my application layout including chartkick and yielding chart_js
application.html.erb (layout)
<%= javascript_include_tag "application", "chartkick" %>
<%= yield :charts_js %>
Can anyone tell me why I'm getting this error and how to fix it? I'd really like to start using Chartkick to generate some simple charts.
Probably you missed loading Google Charts or Highcharts (the adapters).
Try adding this line <%= javascript_include_tag "//www.google.com/jsapi" %> before <%= javascript_include_tag "application", "chartkick" %>. Also check the Installation section in gem's home page (scroll down :)).
To View:
Google Charts
In your layout or views, add:
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
link: https://chartkick.com/#google-charts
I had a similar issue as well when I first tried out ChartKick. The error message that I got is Error Loading Chart: No charting libraries found - be sure to include one before your charts
Here's how I fixed it.
I simply added the following line of code below to the application.js file in the assets > javascript folder.
//= require Chart.bundle
//= require chartkick
Save and refresh, that should fix it.
That's all
I hope this helps.
Another cause of error can be the protocol you use in the javascript_include_tag.
For instance you will get the error Error Loading Chart: No adapter found
if you for instance use protocol https for your website: https://www.example.com
and http in the javascript_include_tag in your code:
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
If above doesn't solve for someone, one additional possibility is a mixed content issue.
You can check this by opening the JS console.
Mixed Content: The page at
'https://site.io/' was loaded over HTTPS,
but requested an insecure script 'http://www.google.com/jsapi'. This
request has been blocked; the content must be served over HTTPS.
Solution:
Just change this
http://www.google.com/jsapi
To this (add s to http)
https://www.google.com/jsapi

Rails - jquery plugin loaded in development but fails to load in production

I have the following in a view:
<%= javascript_include_tag 'scrollable/jquery.tools.min.js' %>
<%= stylesheet_link_tag 'scrollable/scrollable-buttons' %>
<%= stylesheet_link_tag 'scrollable/scrollable-horizontal' %>
Because I have this plugin located in /vendor/assets/javascripts/scrollable/ and /vendor/assets/stylesheets/scrollable/
I don't have any problems in development, but when trying to get into that page in production mode, the JS is not loading. I get this error:
NetworkError: 404 Not Found - https://mysite/assets/scrollable/jquery.tools.min-707bfab8972e8e363a009148db789121.js"
Any thoughts please?
Notice that if I go to: https://mysite/assets/scrollable/jquery.tools.min.js - I can see the code. So I am guessing is some problem with the md5 fingerprint?
sounds like they're not getting precompiled? you can tell by checking if they're listed in public/assets/manifest.yml on production.
if that's the case, and you want to access them via the asset pipeline
did you run rake assets:precompile on production?
did you add the files to the config.assets.precompile array (in config/environments/production.rb)? eg, 'scrollable/jquery.tools.min.js', ditto for css files.
if you don't want to use the asset pipeline, use your own include tags instead of the javascript_include_tag/stylesheet_link_tags, ie <script src="/scrollable/jquery.tools.min.js" type="text/javascript"></script> for the js

Rails Tutorial 3, stylesheet_link_tag generating incorrect link

I've set up application.html.erb to link to a stylesheet using the following code
<%= stylesheet_link_tag 'stylesheets/style', :media => 'screen' %>
However when I load the localhost in the browser window it prints this code
<link href="/assets/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
when I view the file directly I'm shown this error
Routing Error
No route matches [GET] "/assets/stylesheets/style.css"
I've read on some other questions that rails by default looks in public/stylesheets so I'm not sure why it's looking in assets?
I also tried to move the css file to the assets directory just to see if it would work however, it still doesn't work and gives the same routing error.
Been stuck on this for a couple of days and it's really doing my head in so appreciate any help you can give me.
Thanks in advance
Rails 3 comes with a new assets management which is actually one of the biggest pluses.
A guide on how it works is here
So if you have the application.css file in your assets/stylesheets you can simply drop the style.css in your assets/stylesheets directory renaming it in style.css.scss
In your view just leave:
<%= stylesheet_link_tag "application", media: 'screen' %>
Through sprockets the Rails app will load it.

Resources