select_control in merb_helpers
So I'm trying to switch from Ruby on Rails to Merb and...naturally having some difficulties. Now I love the promise of merb, but having to hunt down plugins and documentation is, frankly, a huge bitch. Anyway, I'll try to share pointers in the future for stuff I come across.
Now, on to the heart of the matter. In Rails, when specifying a collection for a select_field, you can specify the collection manually by specifying a array in the format [["key1", "value1"], ["key2", ["value2"]] sort of fashion. However, this won't work for merb -- it expects an array ['key1, 'key2'] that is used for keys and values. But, merb has a couple other properties for select_control, namely :text_method and :value_method. If you pass :first in for :text_method and :last in for :value_method, you'll get the same behavior as in Rails! In other words...
<%= select_control :joinable, :label => "Joinable?", :collection => [['hi', '3']], :text_method => :first, :value_method => :last %>
will display a select field with the text "hi" with the value "3". Enjoy!
Ruby + Ruby on Rails tutorial
Note that this isn't finished and may never be finished, but I didn't want it to stagnate in my Proofs box any longer. So take what you can from it.
You, like me, have probably heard all of the hype around Ruby on Rails. "Convention over configuration" they say, and "Don't Repeat Yourself" are common Ruby on Rails adages. Well, it's true. Creating a new database is as simple as telling Rails your database credentials and typing a single command. Adding new tables takes just a single command.
When I set out to learn Ruby on Rails, I was beset with a bit of confusion. Rails 2.0 was released recently (December '07) and Ruby 2.0 is coming out soon. As a result of the Rails thing, there are a lot of out-of-date tutorials out there. So hopefully this will help someone out there trying to learn Rails right now.
Audience: Some programming experience (preferably web programming), but no Ruby/Rails experience.
Any operating system, but some bias towards Linux/Ubuntu
Goals:
- Set up a development environment for Ruby and Rails
- Write hello world in Ruby
- Learn some Ruby syntax
- Learn some Rails commands
- Write hello world in Rails
- Output data from a database
- Take form data and update a database
- Introduce Rails API for flash, style, javascript
- Introduce flash variables
- Apply style sheets
- Apply javascript files
- Prepare for future (Ruby 2.0)
Finally, this tutorial uses the following technologies and versions:
- Ruby: 1.8.6
- Rails: 2.0.2
- Rake: 0.8.1
- Rubygems: 1.0.1
If you're still interested, read on!
STDERR
So I was trying to capture the stdout and stderr from Ruby from external apps executed with the backtick operator, when Finally I found this page. It's quite useful!
jQuery :(
I wish Rails wasn't so intimately tied with Prototype+scriptaculous. It makes jQuery that much harder to use. Even so, using page << and jQuery is much easier than trying to use Prototype to achieve the same end. Not to be a fanboy or anything, but is there any other library out there that can remove a specific option from a dropdown select box in one short line of code?
jQuery and Javascript Hijacking
I stumbled upon this XSS exploit that Fortify published a little over a year ago that jQuery has failed to account for, even now. Sort of worrisome, since that's the javascript framework I prefer most. Prototype, however, has "fixed" it, as you can tell from the bottom of this page. Anyway, sort of a long read.
Fortify publication (pdf)
Dynamic Methods in Ruby
So this was sort of hard even if it doesn't look like it, but I found out how to do dynamic methods and whatnot. Put this in your class.
def self.my_method(takes, two) define_method "cool_"+takes do |arg| puts two + arg end end
Simple Easy AJAX Form Validation in Rails
I'm using Ruby 1.8 and Rails 2 for this.
Thanks to BigSmoke for both the ActiveResource::Errors hint as well as inspiring me to look for another way.
What this method does is create and populate an error div with the specific error messages that occurred, and then destroy the div when the user submits a valid entry.