ooooh time for a rant. lest i write “how to curb-stomp your macbook Part 2.” I’m also writing this before I’ve figured out the solution so should be emotional 🙂
So. syntactic sugar…fucking a.. is basically syntax within a language that makes it easier to read, so instead of having to type out
array.[](1)
you can just type
array[1]
which can be nice, but there are more “magical” examples that make it pretty FUCKING_IMPOSSIBLE for you to figure out what’s going on from existing code because the code randomly doesn’t follow the same rules in some places. And probably what’s worse, they flat out just omit code ALL_FUCKING_OVER the place.
When passing a parameter into a function, you enclose it in parentheses. Except FUCK PARENTHESES, let’s have it work without em! Sometimes!
This is at least somewhat manageable in Ruby, where you’re dealing with functions and objects etc, but when you’re building Rails apps, the problem is an order of magnitude worse, cuz not only is more shit is hidden but it sets up half the shit for you when you run rails commands and you have no idea why some stuff works because it references code you DIDNT EVEN FUCKING HAVE TO WRITE IN THE FIRST PLACE SO YOU DONT KNOW IT EXISTS!
mother….fucker.
and while I’m ranting, would it be possible to NOT have the same FUCKING word for completely different things in a project? So I’m looking through this sample code for an app that stores a db of movies.
In it is a model file, movie.rb, with a class Movie, a controller file, movies_controller.rb, with a class MoviesController that has an instance variable @movies. Also in the functions on the view file, |movie| is used to iterate on and used as a parameter for the search functions.
So in the routes file when it says:
root ‘movies#index’
resources :movies
what do you think it’s referencing there? controllers? models? classes? variables? pizza toppings?
YEAH I DONT KNOW EITHER!
……. 5 hours later…..
okay think I got a handle on this. sidenote- another thing I hate about rails is that most of my programmer friends hate it and not only can’t help but get elitist and refuse to even try. so i have to ask coworkers, which I really hate.
okay lesson 1- root/resources versus GET
The single most helpful first thing I learned is that this:
get ‘/’ => ‘main#index’
is the same as this:
root ‘greeter#hello’
I don’t think anyone has ever written a rails routing guide IMMEDIATELY after figuring this stuff out from scratch and it becomes so second nature that they forget what stuff can trip you up in the beginning. So then they go, oh root is your main landing page, so just use root. Well fuck you, it’s super helpful to think in terms of the URL path which is literally just ‘/’
Now that we have that, the rest of the explanation follows much more naturally.
So all the routes file does is map 3 things together:
HTTP Methods (GET, POST, PUT, DELETE) :: URL pathways (/, /movies, /movies/new etc.) :: the right controller and method ON that controller.
so get ‘/’ => ‘main#index’ in english means, when the page is requested send them to ‘myapp.com/’ then go to the main controller and run the index function.
the long way of writing a routes file for a single page looks something like this (but with all the HTTP methods and 7 standard controller methods)
GET ‘/’ => ‘main#index’
GET ‘/movies’ => ‘movies#index’
GET ‘/movies/:id’ => ‘movies#show’
GET ‘/movies/new’ => ‘movies#new’
POST ‘/movies/:id’ => ‘movies#create’
and then you’d have index, show, new and create as methods in the movies controller.
BUT rails decided you’re lazy and don’t want to type that so:
resources :movies
will do the exact same thing. The end.
Lesson 2: just kidding you don’t actually need a method in your controller, cuz you’re lazy and you hate parentheses and typing and don’t want to do any fucking leg work to get your app working.
so this: root ‘greeter#hello’ means that the ‘/’ is requested, points to the greeter controller and runs the hello function.
BUT what there WAS NO hello function on my controller. so how did it know? and why if i changed it to greeter#index then added an index function, did it break? feels like that should be MORE right? right? rightrightright?
well, sneaky rails, turns out that if there’s no function in the controller, it sort of implicitly goes to the view files, finds the folder with the same name as the controller and magically knows to renders the “hello.html.erb” file.
so here’s how I broke it down:
This is what I have:
But this also works, meaning the method can be anything as long as it’s the same as the filename in the views folder.
OR if it’s not the same name, you have to define the method that renders the file.
I cannot tell you how much better I feel after finishing this entry. Sometimes it feels like I’m going backwards. I’m simultaneously building complicated shit that would assume I know how this all works, then in trying to replicate someone else’s code and it doesn’t work, and i can’t figure out why, I end up spending 5 hours peeling back all the layers of all this “sugar.”
Fuck sugar. And fuck rails as a first framework. One really should do things the hard way before having the luxury of shortcuts. Tho I guess you could call what I’m doing to learn this almost worse than the hard way?
oh well, I will defeat you rails, if I end up accidentally learning an unnecessary amount of computer science along the way…