Kris
Kris

Reputation: 1423

Calling a method defined in a different controller when submitted form in Rails

I have a form_for tag specified as = form_for [@driver,@driver_availability].This stores the entered data in the driver_availabilities model and calls the create method of the DriversController. Is it possible to make it call some method i define in a different controller but continue saving data in the driver_availabilities model as usual ?

Thank You

Upvotes: 0

Views: 290

Answers (1)

iGEL
iGEL

Reputation: 17392

[@driver, @driver_availability] will call driver_driver_availabilities(driver_id: @driver) for new objects and driver_driver_availability(driver_id: @driver, id: @driver_availability) for existing driver availablities. So either you create a named route routing to the other controller (notice that these named routes are used for index, show, update and destroy as well) or you provide the url option to the form tag:

= form_for [@driver,@driver_availability], url: … # named route or routing hash

I would go with the second option.

Upvotes: 2

Related Questions