Skip to content

Instantly share code, notes, and snippets.

@josephmosby
Created December 15, 2012 16:59
Show Gist options
  • Save josephmosby/4297129 to your computer and use it in GitHub Desktop.
Save josephmosby/4297129 to your computer and use it in GitHub Desktop.
Week Two of Ruby on Rails: Layouts, Templates, Views, oh my!

One of my first projects as a Ruby/Rails programmer is simply getting static pages served on Heroku. That's it. Nothing fancy. I know that some would argue that I should be coming up with an "application," and I'm getting there. A friend had an immediate need to roll his static page over to Rails as he began to plan for more functionality, and I was happy to help to get some practice.

A few things pop into my head here. I have only been programming in Ruby/Rails for two weeks now, and that has been at night after the day job. I haven't even been fully invested in it each and every night, and yet I still feel like I'm growing in proficiency fairly quickly. Rails has its own set of frustrations - but 90% of the time, I feel like this is just how things should be DONE.

However....

Rails is an opinionated framework. It has a prescribed way of doing things, and it doesn't quite care if you don't want to do things that way. I still have no idea how I would directly deal with an HTTP request; I let Rails do that for me. I thought that these opinions would only deal with how my application is constructed - but I am now learning that Rails itself is designed to keep away from what's going on under the hood.

It is well-documented that Rails follows a Model-View-Controller pattern. I am accustomed to a (T)emplate being in there somewhere, coming from a Python environment. I said to myself "oh, Views are like Templates, even though they're a bit more complex." And temporarily, all was good. Then I started trying to serve static pages built out of Bootstrap. And - perhaps most importantly - I tried serving static pages using code that my collaborator had been fiddling with. He had been working on getting Devise up and running, and had set a login/logout button up in his experimentation.

Caring little for my collaborator's code, I blew away his pre-constructed views. (I'm working on my own Git branch as we're getting things up and running) I dutifully created .erb files in place, and redirected my controllers appropriately. I am serving static files! But wait... that login/logout button is still showing up. How is that possible? I knew it wasn't in the Bootstrap HTML I was creating, and I knew that all of the previous views were gone.

When working with Django OR webapp2, I would solve this problem by opening up the template and the view that called it. Eventually, my code itself would reveal that I had called some secondary template file in a prior iteration of the code, and that I need to delete that line. I cracked open my controller and my .erb file, and got nothing. There is nothing in the code that told me where that login/logout button is.

Turns out, Rails has a layouts folder. The login/logout button was placed into a layout.

Rails is not transparent about what it is doing. There is no indication in code that I created that alluded to this layouts folder. rails server is calling some Ruby method to look in the layout folder first, then it stitches in my views. What happens if I install some gem that intercepts that layout/view method and injects its own code because the author thought that would be a good idea? How I would I debug this?

Rails makes me nervous because I suspect I'll have to debug any application in a convoluted manner. I can't immediately tell if my Ruby code is weird, if I have a bad gem, or if I don't know enough about Rails' methods. I'm not sure how I'll solve problems. So, Rubyists, I pose yet another question to you: how do you debug your applications as they grow in complexity? Is Rails simpler than I suspect, and so I will learn its ins and outs quickly? Or is this gem interdependency and aggressive abstraction symptomatic of a framework that consists of bolted-on parts?

@deric
Copy link

deric commented Dec 17, 2012

It is called Convention Over Configuration You can find it all in the Rails code, but for beginner is usually faster to learn those conventions. If you write less code, you have less mistakes to debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment