Search code examples
ruby-on-rails-3restcontrollerdestroy

Destroy-like functionality using a custom controller method in Rails 3


I have a resource called patient_admissions that has all the RESTful routes. It is nested under another resource called patients. I want to add another method to my patient_admissions controller called discharge that updates a field in the model called :discharge_date (with Date.now) and saves that value in the table.

I would like this to work like the destroy method, in that if I have a bunch of patient_admission objects listed in a table in my index view, I could just click on the Discharge link and a confirmation box would appear, I would click 'ok' and then the value would be updated without having to first go to another view and deal with forms.

How can I do this without resorting to something like javascript? Many thanks!


Solution

  • In the rails guide on Routing, there's a section on adding additional restful actions:

    http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

    The example there would translate to something like:

    resources :patient_admissions do
      member do
        put 'discharge'
      end
    end
    

    This will recognize /patient_admissions/1/discharge with PUT, and route to the discharge action of PatientAdmissionsController.

    This will at least allow you to get the routing set up for the action.