Posts

Showing posts with the label ROR

Writing test code for ApplicationController

Did you ever try to write functional test code for ApplicationController? You might have faced such situations where you needed to test some methods of your application controller. Well when you will start testing a method of your ApplicationController, you would probably have a "aha" moment - "If I had a view file for that method!!". Yes the problem is template missing error.  Usually the methods in ApplicationController dont have a corresponding template. Therefore, sending test request to those methods will definitely raise an ActionView::MissingTemplate error. So what you need to do is just tell the ApplicationController - "Please dont raise any template missing exception. I am testing your methods :)". But how will you tell this? Simple, just override the rescue_action method and raise nothing!! Just like the following: class ApplicationController def rescue_action(e) end end Yes, this will keep all the exceptions away from you. Howev...

recognize_path to determine url hash from RESTful url

If you are using RESTful controllers for your application, same url can refer different actions in a controller based on the associated HTTP method. For instance let’s consider a URL http://your.host.com/messages/123 . Any request with this url with HTTP get will be paired with the show method of messages controller. Some other pairs are like HTTP put with update action, and HTTP delete with destroy action. Then how can you get a hash of controller and action from this url? In this situation recognize_path to the rescue. You can use ActionController::Routing::Routes .recognize_path(url, method) where url is the '/messages/123' part and method is anyone from GET, PUT, POST or DELETE. It will return a hash containning controller, action and other parameters, for example :id => 123 in the above case. By the way if you are thinking to trim the first part of the url just use as following url = 'http://your.host.com/messages/123' extracted_part = url.g...

ROR draggable_element with ghosting property in IE

Some days ago I faced a problem of drag and drop functionality in developing Scrumpad . We had to give a way to drag a DIV element from one block and drop to another to make user experience more interactive. It’s really an easy task as we have many rich javascript libraries. Besides ROR provide a rich javascript helper. However we enabled the DIV s to be draggable by the following way: <%= draggable_element ‘element_[id]’, :revert=>true, :ghosting => true -%> Here ‘element_[id]’ refers to some DIV s having id like ‘element_1’, ‘element_2’ and so on... When equipped with everything I checked in firefox and found everything working well. But in IE this code created a huge mess. After digging a bit more I found the problem in the ghosting property in draggable_element function. Actually this property helps to create a replica of the dragging element and move that DIV anywhere else. But IE showed so devotion on it that it was creating a new replica on each movement of ...