I’m finally really starting to get things in Rails. It takes time and doing. So here are my thoughts and I hope these are useful for others just starting out. I’ve said some of this before, but consider this new because my ability with Rails has improved.
Basically, getting it with Rails requires a few things:
You do need to learn Ruby first. You can certainly follow some tutorials and get the basic CRUD app up and running with scaffolding, but you will not get far beyond that without some knowledge of Ruby itself. Understanding Ruby makes understanding the how and why of Rails much clearer. So, do yourself a favor, like I did, and don’t expect much out of Rails until after you’ve put in the time to bone up on Ruby. Some people don’t do this and they end up relying on plugins and copying code without understanding it. Ruby is different from other languages. Rails is different from other web frameworks (except for Symfony, the very interesting PHP framework from France that tries very hard to borrow good ideas from Rails and implement them in PHP). Many things in Rails are possible because it is Ruby. (even the Symfony people explain this)
Ok, so now you’ve got Ruby. Now get Rails. Well, you can and should start with the Agile Web Development With Rails book from the same people as the “pickaxe” book, Programming Ruby. But don’t expect to get it after working through the tutorial project. There is only one project there. It will give you a fast, broad introduction to a huge amount of the tools and technologies in Rails. It’s too much though, for most people to really get all at once.
So what helps? Do more tutorials, so that the Rails conventions start to sink in and you can focus on the how and why stuff and then move on to making new things. One good one is the tutorial “Rolling With Rails” at the OREILLY OnLamp site. It makes some things clear in a different way. But a book that was recommended to me is the SitePoint book, Build Your Own Ruby On Rails Web Applications, by Patrick Lenz. After working through the others, this book makes things clearer. This book doesn’t assume any prior web application development. It does assume HTML and CSS ability. And this book gets my humble approval.
What’s that you say? You don’t want to spend money on books?! Then YOU ARE A FOOL. If you want to do anything and do it well, particularly anything technical, you need reference material at the least. You can not rely on the web alone. You wouldn’t study German or Japanese only online, so don’t try to do that with programming either. It doesn’t work.
Build your own library of reference materials, both in print and online. Nothing beats having the pickaxe book or the AWDWR book laying around for toilet reading. You will find something new and useful every time you look in them, and build your knowledge over time. Any programming framework is built on top of a language. Both take time to learn and more time to become skilled at and still more time to master. Frameworks in particular require lots of looking things up in documentation and in books. You can’t expect to know it all or just figure it out.
Anyway, I’m now starting to get it. I found my Ruby skills very useful today.
In my RHTML file for showing an item’s detailed record, I had already added the code for some associated tables that I hadn’t yet created. Thanks to the concise nature of Ruby and the wonderful methods in the core language, I was able to put in some conditionals that simply check to see if a method is defined. Rails automagically creates many CRUD methods for each model, and a model that has_one or has_many of another model will inherit its methods. It’s a wonderful system, these ActiveRecord associations. With this, and my conditional to see if a particular, automagic method is defined, I can indirectly check to see if the table or its data exists or not. This is the power of Ruby’s introspection and Rails’ good design and my knowledge of Ruby.
Here’s an example:
I have a model called Assets in asset.rb and a controller called asset_conroller.rb and a table named assets, of course. I also have a model in mind called Thumbnails but haven’t yet created a controller or a table. Thumbnails’ model thumbnail.rb will have the line belongs_to :asset and Assets’ model will have has_one :thumbnail or maybe has_many :thumbnails but I haven’t decided yet. But I’m building my show.rhtml file for showing an asset right now. I want to include it in the rhtml, so I will have it in mind and yet I don’t necessarily want to have to comment it out or anything. (commenting things out in ERb is awkward at best and is one of the weaknesses of ERb itself)
So here’s what I did in my rhtml file…
<% if defined?(@assets.thumbnail) %>
<div>
<strong>Thumbnail:</strong>
</div>
<% end %>
There’s no code inside the conditional, yet, but if it is there and it uses the inherited thumbnail methods, I don’t have to worry about it!
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment