Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Thursday, October 23, 2008

Named Routes: Anchor

If you're using named routes and want to add an anchor to your automatically generated URL, there's a very simple way to do it:

products_path(:anchor => 'my_anchor')

It's that simple!


W

Friday, March 28, 2008

Named Routes: url vs path

So I spent a good 30 minutes trying to figure out why my links were missing the host part of the URL.

What I was trying to do was send emails using ActionMailer and generating urls inside the ActionMailer instead of the ActionController. The documentation says that you can use url_for and named routes if you do what it says.

Well, for a while there, I was completely stumped as to why my named routes were missing the host part, until I realized I was using the path method for named routes instead of the url method:

users_path #/users
users_url #http://www.mysite.com/users

The difference is obvious. However, if you're used to using path instead of url, you may be stumped as well. Hopefully, you guys won't have to go through the trouble I did ;)


W

Monday, November 5, 2007

RESTful Cheatsheet

I found a very handy cheatsheet that I would like to share with you guys. I've found it very helpful through my RESTful endeavors:

http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf

Enjoy!

W

Saturday, October 20, 2007

RESTful Development

Along with Named Routes comes RESTful development. So what is RESTful development? Well, I'm not even going to try to define here. A very uesful link is the RestWiki, which talks about what REST is.

I must say that I quite agree with this architectural style. I can see how it really tries to create uniform interfaces so that different applications are able to communicate with each other. Especially over the World Wide Web, as they say, RESTful development is almost natural. However, I think we have been developing so long without REST that it will be awkward at first to get use to.

RoR, along with Named Routes, supports RESTful development quite well. Each controller, because it represents a model, will have a Named Route corresponding to it. Furthermore, this restricts the controller to having only 7 actions. Yes, that's correct. I said 7!

index
show
new
create
edit
update
destroy

This seems very limiting, but when we're dealing with 1 particular resource, these 7 actions are, in fact, quite sufficient. There may be 1 or 2 extra actions that may need to be customized, but usually, these will be all you need!

With REST, it is easy for other applications to easily hook into yours. With RoR, it is easy to return different formats to the caller, such as XML. A really nice explanation can be found here:

http://softiesonrails.com/tags/rest

Well, have fun poking around REST. Hopefully, this will convince you to try it out for yourselves as well!

W

Friday, October 19, 2007

Named Routes

The RoR project contains a file called routes.rb.

This file handles the routing of URIs to controller actions. Simple things can be done like routing www.mypage.com to some home page, or even a login page such as www.mypage.com/login by doing:

map.connect '', :controller => 'user', :action => 'login'

Something cool here can be done:

map.login 'login', :controller => 'user', :action => 'login'

gives us a shortcut to the login page. In our controller or in our view, we can now use:

login_url # www.mypage.com/login
login_path #/login


This is pretty neat as we can change all our paths to variable paths, and so if we decide to change the structure of our URIs, we only need to change them in one place!

Slightly more advanced are things called Named Routes. By specifying:

map.resources :user

we get several shortcuts that can be used in conjunction with HTTP methods (GET, POST, PUT, UPDATE). For example, we know have access to:

edit_user_path(@user.id)

which will give us the path /user/:id;edit. This will automatically call the edit action in our user controller. Pretty cool huh?

For more in depth information, try this website:

http://ryandaigle.com/articles/2006/08/01/whats-new-in-edge-rails-simply-restful-support-and-how-to-use-it

Named Routes naturally give way to RESTful development. If you don't know what that is, I recommend reading up on it. I'll talk about it sometime later, so check back!

W