Well, I hope not too bad. At least the link shouldn't have been the problem.
We will pimp our simple example a bit and in the next post I hope we could begin with some serious stuff.
But in any way, I have to say something on the structure of a typical camping-file. As you might already have seen in Rails, camping has got at least a Controller and some Views, which contains a layout-method. The regular expression in class Index < R '/' is a very basic one and used to invoke the Index-controller by the URL '/' using the 'get'-method.
Since coding is a lot funnier than theory, lets start with some lines.
Look again at our first verison of our :Simple. Have you already added a new page? If not, we do that now. At first we add a new controller for our greeter.
class Greeter < R '/greeter'
def get
render :greeter
end
end
We use the regexp '/greeter' to link the URL "/greeter".
Then we add a view for our hello-greeter:
def greeter
p "camping rocks. Spread it!"
end
So thats it. We have now another nice greeter! Just add 'greeter' to your URL (typically localhost:3301/greeter).
But imagine, you want to add a bunch of different greeters, maybe to say "Hello", "Goodbye" and "Yo Man!". It would be annoying to add a Controller for ervery message. And there is a remedy for this problem: Tune the Regexp!
If we sum up all the three ways of greeting in one Controller we will have to add the following lines in Simple::Controllers:
class Greeter < R '/greeter/(\w+)'
def get message
render message
end
end
The \w+ returns ervery letter-character after the slash.
Now we want to get the three greeter-views:
def hello
p "Hello Guyz!"
end
def goodbye
p "Goodbye and good luck!"
end
def yoman
p "Yo man, check it out, man!"
end
Wow! Now lets try it with /greeter/hello. Cool, isn't it? Well..

0 comments:
Post a Comment