Tuesday, February 6, 2007

#Partials start here

Let's tune our simple Camping-site with some interactivity! Imagine you want to greet not just yourself, but maybe your friends or your grandma!

To greet your friend Jimmy, we have to tell our "app" about him and his name. And we do that with a little input-field. In both Rails and camping this is done with so-called "Partials". As you might guess, these partials aren't complete webpages. They are only fragments of a page, in our case an input-area. So lets define a partial with an input field.

def _greet
form :action => R(Greeter),
:method => 'post' do
label 'Persons you wanna greet:',
:for => 'names'
br
input :name => 'names', :type => 'text'
br
input :type => 'submit',
:value => 'Greet!'
end
end

Now we've got a partial with a button. If the button is pressed the method 'post' in the Greeting-controller will be called. So the next step is to define this post-method:

class Greeter < R '/greeter/(\w+)', '/greeter'
def get message
render message
end
def post
@names = input.names
render :greetings
end
end


As you might have noticed, we had to change the Regular Expression too, since we don't pass the input over the URL and will use the URL '/greeter'. We define a class-variable @names which contains the names you typed in the _greeter-partial. This variable is also accessible from the 'greetings'-view. So lets define it. You will see some pretty cool Ruby-maigc now...

def greetings
@names.split(", ").each do |n|
p.hello ("Hello " + n + "!")
end
a("Back to input", :href => R(Index))
end

Well, if you have already some experience with Ruby thats just erveryday-stuff. But if you came from say Java this is a reason alone to dive into Ruby.
We split the given String containing the names of the people you wanna greet. So you see already the way you'll enter a list of names ("Jimmy, Paul, Mary").
The last task is to update the index-view by adding the _greet-partial.

def index
p.hello "Welcome on my greeting-engine!"
_greet
end

Now we are done! Thats the ruby-/camping-way of webcoding. Much cooler than HTML, isn't it?

As we've reached a certain level of complexity, I strongly recommend to take a break and do some projects for your own. By writing your apps you'll learn a lot. With the toolbox you got with our Simple-project you can do more than you might imagine at the moment. So feel free and start building your own camp in the wild!
Just as an inspiration for some praciticing:
  • a little Calculator with a webfrontend

  • a Tic-Tac-Toe for your local network (if you avtivate websharing, your app will be available over your whole local net)

  • Everything you could dream of (please share your ideas with a comment!)
One thing at the end: Please don't forget that you have the power of Ruby right at your fingertips. Ruby is one of the coolest, easiest and most powerful script-languages. And what if you add a genious web-microframework...

2 comments:

Anonymous said...

great work. Im so glad I found this site.

Anonymous said...

Great read! I want to see a follow up on this topic =D


http://insurancetext.info