Monday, April 21, 2008

Ruby on Rails Best Practices

I have worked with ruby on rails framework 1 year, and my friend and I find some ways that might be best practices in this framework.

1. If your logic code that interacts with database is complicated, you should make it as a class method so that this method could be reusable by other developers in the team. You can return true/false to indicate this process is successful or failed if you don't want any validation errors object. You can also return object/an array of object to get validation errors.

2. Don't use render :update do |page|....end which is the bad thing because it returns a javascript code to the browser. And don't use link_to_remote :update => "id" because this code also generates javascript code back to the browser. Instead, use render :partial => 'update' to render the plain html code back to the browser and use oncomplete callback to replace the old content with the new result such as: document.getElementById('id').innerHTML = request.responseText;

2 comments:

Alno said...

About 2:

Do you mean not using RJS templates at all? Sometimes, it's very convenient.

And, why return javascript is bad?

chamnap said...

Well, it depends on yourself whether it benefits you more. Personally, when the server always send back with javascript code this way, it makes my web application coupling to this resource.

Subscribe in a Reader