0

I hope everyone is having a great day. In my pursuit of knowledge, I've ran into a huge stumbling block.

I'm following this tutorial. I'm using the most up to date everything, I think he is in rails 4 and I'm in rails 5.

https://youtu.be/GY7Ps8fqGdc?t=703

By no means is watching the tutorial necessary to understand my question. Right where I linked, the only thing he has done besides generate a simple rails app, is generate a controller like this:

rails generate controller welcome index

Then right where I linked into the video, he goes into the routes file and uncomments a line that isn't present in my rails app, so I just wrote it by hand

root 'welcome#index'

And it fails when I try to load localhost:3000, throwing a jsexception "TypeError: Object doesn't support this property or method"

Is there some functionality that has been deprecated or something? Why isn't this working? I've done the tutorial up to where I've linked at least 5 times. I have tried very hard to resolve this with no avail.

Oh, and before someone tells me you shouldn't route like that, I've read that before, but he's just trying to get the viewers to the point where they can execute ruby code on a web page, he covers routing in detail later, but I can't get there without beating this because the lesson relies on me getting this first.

Please, oh great forum, grace me with some answer and I will imbue you with an upvote and great thanks.

Also, on a side note, if anyone could recommend learning resources that have helped you get past beginner with this framework, I'd be happy to accept advice in addition to this question being answered.

EDIT: to include requested information

the routes.rb file

Rails.application.routes.draw do
  get 'sausage/tuna'
  root 'sausage#tuna'
end

the error output

One last piece of information, probably obvious, I call my things funny names so the generic "welcome" and "index" don't confuse me. I draw inspiration from the new boston and called mine 'sausage tuna' instead of 'welcome index'.

Thanks again for helping me out. I really want to learn this.

geekTechnique
  • 850
  • 1
  • 11
  • 38
  • 1
    Can you post more of the error stack, a js error is not because of this root route, unless there is something on that page specifically. It would help to post the whole routes file and the whole error output (there isn't anything in the controller method correct?)" – Rockwell Rice Nov 10 '18 at 21:21
  • Please share your entire `routes.rb` file. – moveson Nov 10 '18 at 21:21
  • Sure! I'll edit my post in just a second. Thank you so so much. To answer the first question, we have not touched the controller file at all yet. Will update in minuets. – geekTechnique Nov 10 '18 at 21:26
  • 2
    Whatever the javascript error is, it's not directly related to the steps you've shown. I'm able to create a new rails app, generate the controller, and add the root route as indicated and the page loads successfully without any javascript errors. Start over and just create a new application with "rails new blah". Then start the server and load the home page in an incognito window (no plugins, ad blockers, etc.) Does that load successfully? If so, you can proceed step by step from there. – showaltb Nov 10 '18 at 21:28
  • Hey showaltb, Thank you so much for the thoughtful response. I've now tried to build a new app, the same way, ran it in a private tab with no plugins in different browsers as well as reinstalling rails from scratch. I'm still working on this issue. Thank you so much for what you've helped so far. – geekTechnique Nov 10 '18 at 21:46
  • And just to let you know, if I comment out that one line, #root 'sausage#tuna' the main page loads telling me I'm on rails. That one line of code is what makes the difference to me. – geekTechnique Nov 10 '18 at 21:52
  • What happens if you remove that `get` route above the `root`? And, what is the `get` route suppose to be doing? It looks like it links to the same controller and method as the `root`? – Rockwell Rice Nov 10 '18 at 22:05
  • Thanks for your comment Rice. It was there by default when I generated the controller. Nothing changes when I comment out that line. Exactly the same error about an Object doesn't support this property or method. As far as what it is doing, I'm clueless, seriously been learning rails for 2 days. Just trying to follow his tutorial, and it's there in his. Thanks again for trying to help. – geekTechnique Nov 10 '18 at 22:17
  • @RockwellRice the `get` route is added by the generator when you generate the controller. If you use `rails g controller welcome index`, it adds the `get 'welcome/index'` route, which allows that action to be accessed at http://localhost:3000/welcome/index. Nevertheless, the extra route is not causing the javascript error. There is some other problem/corruption happening. – showaltb Nov 10 '18 at 22:19
  • @showaltb . Ya I understand how it works, I just always delete the default route when I turn it into the root, I never saw two routes pointing to the same thing like that so I thought maybe the overriding could be creating some kind of weird error. – Rockwell Rice Nov 10 '18 at 22:25
  • 1
    @TerryDorsey ah, I didn't realize it was an ExecJS error. I thought it was an error in the browser. ExecJS uses a javascript runtime to compile your coffeescript files into javascript. Googling for "ExecJS::ProgramError" may help. Also, try adding `--skip-coffee` to the `rails new` command to see if getting rid of coffeescript gets you past this problem. If so, you can deal with ExecJS later. – showaltb Nov 10 '18 at 22:33
  • @showaltb YOOO that worked!! Thank you so much. Since you clearly know your stuff, I want to ask what do I do when I need coffee to work? Will I not be assigning a root this way later on? For further reference, I just installed rails on my gf's completely different laptop to recreate my issue and got the exact same ExecJS error. I mean this is 100% repeatable on 2 completely different environments. One last thing, if you copy that thing about --skip-coffee into the answer field, I'll happily approve it as best answer. Thank you so so much. This was making me pull out my hair. – geekTechnique Nov 10 '18 at 23:07

2 Answers2

1

Ok the problem seems to come from ExecJS, which is basically the gem that deals with JS within Ruby.

The offending line is : <%= stylesheet_link_tag 'application', media: 'all' %> (as you can see I am missing the turbolinks bit because I am not using turbolinks)

What this means? Basically this line is for a dedicated sheet of JS code attached to this page.

where is this code located ? In application.html.erb. application.html.erbis the default view layout. It means that every view triggered by your code inherits from this 'master' view.

So basicaly your routing is good if the error goes to this stage. Your Sausage#tuna action is triggered and then the `tuna.html.erb" view is rendered but with an error on this master view.

Basically because you have an error on application.html.erb, you will get the same error for every action of any controller and every view triggered.

Now what the problem ? Well I never encoutered this error but skipping coffee seems to solve your problem. Though in my case I have gem 'coffee-rails' and some of my JS files are actually "coffee.erb" files and I don't get the same error. So i guess it is kinda more complex than this..

But in order for you to understand Rails better , this line, as I said earlier, is compiling some JS and attaching it to the master view file (then to every view of your application)

Now if you want to know what JS will be compiled into this separate sheet, looks at the file app/assets/javascrips/application.js.

You should get something like :

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery3
//= require jquery_ujs
//= require bootstrap
//= require cable
//= require rails.validations
//= require footer.js
//= require serviceworker-companion

Everything looks like it is commented out, but actually the last lines with //= require are actually gathering some JS from different gems. (Jquery, ...)

In my case : Jquery, Bootstrap, actioncable .. which is the Js I need on every page of my app...

Regarding your question about a good tutorial, the one I prefer is the getting started tutorial on Rails website : https://guides.rubyonrails.org/getting_started.html

EDIT

My mistake. It is the stylesheet that creates a problem. Which I am even less sure what could cause this because ExecJS is dealing the Js code in your app, not the CSS. The stylesheet work exactly the same as said above for application.js. It links some CSS stylesheets to every of your application pages.

Here is mine app/assets/stylesheets/application.css

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require navbars.css.scss
 *= require footer.css.scss
 *= require cookiewarning.css.scss
 *= require_self
 */

@import "bootstrap-sprockets";
@import "bootstrap";

As you can see, there is the navbar css and the footer css plus some other CSS that appends on everypage of my app.

(And thanks for accepting my answer as the answer but it is definitely not, just wanted to explain you what were things doing :) )

Maxence
  • 2,029
  • 4
  • 18
  • 37
  • Amazing answer, but I think you left something out after "the offending line is:" – geekTechnique Nov 10 '18 at 23:28
  • 1
    Yep I have corrected (I think). This is definitely not answer, it doesnt give any answer on why JS is stumbling on your application.css stylesheet. But it is rather a quick context explanation. For the best tutorial I definitely recommend the 'getting started' mentionned above. – Maxence Nov 10 '18 at 23:36
  • I accepted it as the answer because it got moving forwards in my Rails tutorial, and it was very thorough. I appreciate what you've done for me so far. Maybe I'll downgrade to Rails 5.0 to see if this issue persists. My application.css file is a little different than yours, with all the commented out stuff, but it doesn't have the @import commands at the end like yours does. – geekTechnique Nov 10 '18 at 23:44
  • To help us better understand the problem, your server logs are more insightful. Just copy the last lines of Webrick or Puma, it will give more info. Re the import, it's related to Bootstrap. When you install a new gem, you are sometimes asked to add some code at different places as installation guidelines. I just do what they say :) (And also there is no reason to downgrade Rails version, a new install should work all the time flawlessly. I had quite a few problems with Windows actually ... but if you can boot your server, Rails is kinda Ok) – Maxence Nov 10 '18 at 23:49
  • Regarding your problem, there is this popular thread also : https://stackoverflow.com/questions/28421547/rails-execjsprogramerror-in-pageshome pointing to different problems, some specifically linked to Windows OS. Some recommend installing NodeJS alongside `gem 'execjs'` (I remembered having to install NodeJs at some point on my plateform too) – Maxence Nov 11 '18 at 00:01
  • Rails (or sprockets actually) runs the css through execjs as it uses tools written in JavaScript to concatenate and create source maps etc. – max Nov 11 '18 at 09:57
0

For anyone experiencing this issue, reinstalling node seemed to solve my problem.

geekTechnique
  • 850
  • 1
  • 11
  • 38