52 Weeks Of Code: Week 7 Ruby On Rails in 52 Weeks Of Code

Written by kishore

[b]Introduce yourself to Ruby on Rails[/b]

[img]http://imgur.com/CQqlX.png[/img]

[b]Challenge:[/b] Introduce yourself to Ruby on Rails by creating a simple web application.

Rails is a framework for the Ruby programming language and is focused on MVC (Model-View-Controller) architecture for programming. After spending a few months learning and playing with Rails, and then returning to do some C++ programming, I realized how much Rails had affected me. My code was clearer and much more organized. I think we can all benefit by just introducing ourselves to Ruby on Rails. I challenge you to try out Rails and write a simple web application. (maybe use the Twitter API that you looked at in a previous challenge)

[b]IDEAS[/b][list]
[*]Create a small personal blog (very easy in Rails)
[*]Create a number guessing game (always fun for learning a new language)
[*]Create a web application that sends you an email (or anyone who enters an email address)
[*]Create a user registration/authentication system (little more advanced, but still doable)
[/list]

[b]RESOURCES[/b]
One great way to learn Rails is just to start playing with it. Rails tries to take complicated ideas and make them easier with the tools it brings. If you're looking for information or help with your challenge, then I first suggest the [url="http://www.dreamincode.net/forums/showforum70.htm"]Ruby[/url] programming forum right here on Dream.In.Code. You can also look at [url="http://railscasts.com/"]Railscast[/url] which has a plethora of screencasts for doing all sorts of cool stuff with Rails. Also, if you plan on doing the blog idea, [url="http://media.rubyonrails.org/video/rails_blog_2.mov"]here's a screencast of how to create one in 15 minutes with Rails[/url]. And of course, the [url="http://rubyonrails.org/documentation"]Ruby on Rails Documentation[/url].

[b]HOW TO GET STARTED[/b]
Getting Rails set up to develop with is fairly easy and only requires downloading one or two things, depending on your OS.

[b]Windows[/b] users have [url="http://rubyforge.org/projects/instantrails/"]InstantRails[/url], which quickly and painlessly gets you going. [url="http://blip.tv/file/49059"]Here's a simple screencast on setting it up[/url].

[b]Apple[/b] ships their latest OS X releases with Ruby set up by default, but may need updating. Try the following commands in a Terminal window:

[code]sudo gem update -system
sudo gem install rails[/code]

Once you get Ruby on Rails set up, you can create a project with the following command:

[code]rails TestApp[/code]

You now have your basic Ruby on Rails application called TestApp set up with folders and everything. Test it out by going into the new TestApp directory and typing:

[code]script/server[/code]
or
[code]ruby script/server[/code]

This will start an internal web server to host the Rails web application. After it's running, point your browser to [url="http://localhost:3000"]localhost:3000[/url] to view the page.

Now let loose and see what Ruby on Rails has to offer you.

Thanks to Skaggles for submitting this challenge.

Original post:
52 Weeks Of Code: Week 7 Ruby On Rails in 52 Weeks Of Code

Feb
15

Rails: Routes Explained – Part 1 in Ruby Tutorials

Written by kishore

[b][size="3"]Rails: Routes Explained – Part 1[/size][/b]

Routes are basically how a Ruby on Rails application reads a URL. Routes can be modified by editing the file [i]routes.rb[/i] found in the config folder of your Rails application. By changing this file you can set how URLs are handled by your application. One thing to note, however, is that the [i]routes.rb[/i] file is prioritzed from top to bottom. Keep this in mind when planning out your routes.

[b][size="3"]Basic URL Routing[/size][/b]

The most basic route, and by default already added to a Rails application, handles most of the URL calls you will receive:

[code]
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
[/code]

The first line tells Rails that for a URL like 'http://yourdomain.com/home/about' will access the 'home' controller and run the 'about' action within that controller. It basically breaks the URL down for your application. The first part of the URL after the domain name will be assigned as the [i]controller[/i] and the second is assigned as the [i]action[/i].

The second line adds in the id parameter, which will be passed to the action as a parameter. An example of a URL that uses the second route would be 'http://yourdomain.com/users/show/1'. Since this URL has an added '/1' the first route will not work for it, so Rails will continue until it finds one that does, or error out if it doesn't. It will find that the next route can handle this URL. This is how the [i]routes.rb[/i] file is prioritized.

Say that you get a URL like 'http://yourdomain.com/post/show/12' the route will broken it into:

[code]
params = { :controller => 'post',
:action => 'show',
:id => '12'
}
[/code]

A problem with these default routes is that they're not often friendly for users and not very SEO at times. For example, if your login or logout actions are in a controller called user, then your users will have to access these with URLs like '/user/login' or '/user/logout'. You can simplify these URLs by setting a few routes with patterns:

[code]
map.connect '/login', :controller => 'user',
:action => 'login'
map.connect '/logout', :controller => 'user',
:action => 'logout'
[/code]

Now a user can access the login and logout actions by using the URLs '/login' or '/logout'.

The '/login' tells the appliation what pattern to look for in a URL. In this case it's looking for 'http://yourdomain.com/login'. When the URL is accessed, the application now knows to route this request to the 'login' action in the 'user' controller. This may seem pointless to remove only one part of the URL, but the impact for the user is much larger than you think. Most users expect a URL like 'http://yourdomain.com/login' to let them login, but if you don't route it then it may try to access a controller named 'login' and the user will get a page not found error.

[b][size="3"]Setting a Default Page[/size][/b]

You can also change the default root page of your application by adding the same map line with an empty URL pattern:

[code]
map.connect '', :controller => 'home',
:action => 'index'
[/code]

Now, if your application is accessed via just 'http://yourdomain.com/' your application will know to run the action 'index' from the 'home' controller. Of course, you would change the controller and action to whatever page you want visible. Just be sure to delete the [i]index.html[/i] file from your public folder. By default, Rails will show this file as the root page if it exists.

Another option for specifying a default root page is the line:

[code]
map.root :controller => 'home',
:action => 'index'
[/code]

Either of these two will work.

[b][size="3"]Making URLs Look Goooood[/size][/b]

Another way to my your URL more friendly is by adding parameters into your route:

[code]
map.connect '/archive/:year/:month',
:controller => 'archive',
:action => 'list'
[/code]

Here, we've added in 'year' and 'month' to the route. So, a URL like 'http://yourdomain.com/archive/2010/01' will route to the 'archive' controller and pass the year and month as parameters to the 'list' action. The list action can then use these parameters, along with some code, to retreive all blog posts from that month. It will be broken down into:

[code]
params = { :controller => 'archive',
:action => 'list,
:year => '2010',
:month => '01'
}
[/code]

[b]In closing,[/b]

As you can see, routing in Rails is as simple as a few lines of code. So far, we've seen how to set the default page of a domain, make a URL look more friendly, and tell the application how to route basic URLs. There are more ways that you can do routing in Rails and I encourage you to look into them. I will be covering more about routing in the next part.

[b]Here are some things to try on your own:[/b]

[list]
[*]How would you route the URL 'http://yourdomain.com/profile/2' to the 'show' action?
[*]How would you route the URL 'http://yourdomain.com/about' to the 'home' controller?
[*]How would you route the URL 'http://yourdomain.com/user/my_user_name' to pass the parameter 'username' to the 'show' action?
[/list]

Thanks for reading. This is my first tutorial so I hope it was informative and helpful. Enjoy!

Original post:
Rails: Routes Explained – Part 1 in Ruby Tutorials

Feb
08