Monday, March 21, 2011

Rails work, refactoring and Frankensteining

Still working on the blog app. Today I created a new model for comments and went through tying it to posts, then editing the code to make use of partials. Using partials in this way seems to fit better with Ruby than writing a laborious chunk of code.

So here's the original code:


And this bit is going in with the rest of the code that displays posts, so it's feeling crowded. But then I split it off into a partial, which I'll call with a much slimmer bit of code.

Then here's the partial:


It's basically the same code as before, but it's been sectioned off and put into the views directory. Now it can be called like so:


Look at that tiny little code! And all it took was a little refactoring and reorganizing and that bit of code for the view is easier to read and understand, and the partial is easier to access in case it needs to be edited later.

This method of pulling a bit of code from another file reminds me of the scraps of php I used to use back in the day. Ruby's kind of the Frankenstein of coding languages, anyway, right? But it only used the good parts!

1 comment: