custom actions for inherited_resources
-
11-03-2021 - |
Question
I have 3 custom actions for my controller and was hoping that each of these use the resource object that inherited_resources gives us. So basically, instead of:
def cancel
@job = resource.cancel!
end
def restart
@job = resource.restart!
end
def start
@job = resource.start!
end
I want to just skip that by:
def cancel
@job.cancel!
end
def restart
@job.restart!
end
def start
@job.start!
end
Problem with this is @job comes out nil. So I checked the documentation and found out about custom actions. So I added this to my controller:
custom_actions :resource => [:cancel, :start, :restart]
but @job still is nil. I also tried:
actions :all
to tell IR to apply resource to all actions and it still doesn't work. What am I doing wrong?
Solution
You have to wrap the call like this:
def cancel
cancel! do
@job.cancel!
end
end
This causes IR to run and then yield control to your block (with the resource already set).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow