Adventures in Coding, Pt. 2

My last post about my coding adventures was destroyed by my negligence. Let’s hope this doesn’t suffer the same fate.

So last night, since I was able to work on the computer without a stormborn power outage, I fired up the second lesson of my Ruby on Rails class. The first lesson was centered around generating a new app, which led into a breakdown of all the bits and bobs of Ruby – gems, gemfile, some brief examples of code, all that stuff – as well as the essentials of uploading app frameworks to GitHub and Heroku.

The second one is building pages.

HTML and CSS, as I was telling someone, rely a lot on creating pages and stylesheets as if they are each different documents. Essentially, that doesn’t change with Ruby, but Ruby makes it a lot more convenient – and fast, I think. Whereas in HTML and CSS, I’d find myself opening a new document in Sublime Text, then editing it through that, and saving it as either .html or .css, Ruby has a command:

[current directory] $user generate controller Pages [name of page]

So, typing that into Terminal gives you a shiny, new, blank html page to work with. After editing the pages controller file in the database – changing definitions and actions so the app directs to the correct pages – you can have a url point to “home” rather than “home.html”.

This makes me grin. I don’t know why – exactly – but I had wondered why some pages allow that to work without a file extension, whereas others don’t. And it’s all through the power of Ruby. [Collective “Ooooh” and “aaaaah”]

Further, all the messing around with copying and pasting navigation bars, logos – well, everything that’s not the page-specific content, really – can be accessed using partials. Partials are bits of code that look like:

<%= render ‘[containing folder/filename (i.e. ‘header’)]’ %>

What that’s doing is telling the page to render a certain bit of code at a certain location, and spit that out on a page. You do that by putting the above code on a layout page, which controls the default view of every page.

Everything that’s not defaulted into a page is plopped into that layout by

<%= yield %>

From what I saw last night, that looks like it’s included by default when you generate a new page controller. But if you’re reading this, and you know that’s wrong. Let me know!

So, basically, all you have to do when you generate a new page is write in the HTML for the content of the page.

My God, it’s so easy!

(That said, I think I’ll still keep using <a href=”…”></a> for links. <%= link_to ‘…’ ‘…’> just feels wrong.)

Of course, there are the details to work out, like defining the pages and controllers, and all of that, but man. I get why people scoff at Ruby, but it seems like doing so would be like looking down on people for driving a car when they could be riding a horse. (Or flying across the country when they could be driving…)

So excited, you guys! Just wish I could rewatch these videos to hammer in the ideas, but nooOOoOOooOOooOOo. I should be working. Thanks, The Man.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s