Skip to content

Instantly share code, notes, and snippets.

@josephmosby
Created December 15, 2012 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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?

@r4vi
Copy link

r4vi commented Dec 15, 2012

The cost of getting up and running with rails verses node in this case but you can subsitute node with Sinatra or Flask.

cost

@mbadolato
Copy link

You should probably start off with going through the (free) Rails Tutorial book at http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

@josephmosby
Copy link
Author

@mbadolato - I have been going through the guides.rubyonrails.org tutorial. I'm sure that eventually I would have figured out what was going on by using a tutorial; my point is that it is not readily apparent from the code itself.

@nanaya
Copy link

nanaya commented Dec 16, 2012

As for the layout problem: request goes to routes which decides which controller to use. Most controllers inherit ApplicationController (in application_controller.rb) which usually has layout "<layoutname>" declaration. When undeclared, it defaults to 'application' following same principle as render - when there's no specific action for the controller or simply no render declaration, it renders view from same name of the action.

@brand-it
Copy link

If you want to debug code that is not terrible complicated. However it depends on what peace your are trying to debug. Most people use rails console if they really want to get down to what rails is doing. This mostly will test your methods and there outputs plus it lets you make some code that would normally be hard to tell exactly what its output would be.

However if you are having problems with the layouts that is just something you have to read about. Try one of the o'reilly books they go over the layout and the MCV framework that rails uses. Also check out rails cast another amazing learning tool. Of course I would recommend reading the book before rails cast. http://railscasts.com/

@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