Add the action to your controller. E.g.:
def custom_action render json: @your_model.output end
Add a view template for your action in the model's views directory (e.g. custom_action.html.erb)
Add a route to your new action in config/routes.rb. If it will act on a specific instance of your model, then something like:
resources :your_models do member do get 'custom_action end end
It could also be a put action, or whatever you want it to be.
If it is an action like :index
that doesn't set a particular instance, then:
resources :your_models do
member do
get 'custom_action
end
end
With those three steps, you should be ready to go. You then should have access to the helper custom_action_my_model_path(@my_model)
or custom_action_my_models_path
, however you have set it up.
Make sure that you modify any validations or filtering at the top of your controller, if applicable. If you call the action through a link or a button, make sure you specify the right method (I think link_to assumes :get as a default and button_to a :put; you can just add the statement method: :get
or method: :put
as an argument if needed).