How to Add a New Action to the Existing Controller

How to add a new action to the existing controller?

Add a new action is simple. All you have to do is add a method on your controller, like, for example:

# app/controllers/dummy_controller.rb
def get_back
logger.warn "It works!"
redirect_to :back
end

Now, to be able to access this action throgh a URL, you need to have a route for that. This is done in your config/routes.rb file. You can add it as a hard route, like

get '/go_back', to: "dummy#get_back"

This is the simplest possible route. But you might want it to behave like a restful route. This is useful if you are doing an action over one or more models. So in your route file, you will have something like this:

resources :dummy do
collection do
get 'get_back'
end
end

This allows you to accept a get method over a collection. You will have the helper dummy_go_back_url, and to get to this page the url is /dummies/go_back.

This is for acting over a collection of resources. If you are acting on one specific object, you should specify a member action:

resources :dummy do
member do
get 'get_back'
end
end

Since a member action is for only one object, you will have a url like /dummies/123/go_back. This automatically will set the variable params[:id] in your controller to 123, allowing you to easily fetch your object. Also, the helper method dummy_go_back_path is defined, and received one object or id as parameter to generate the correct url.

These are the most simple routes you can have, but you can look in routing outside in from rails guides as a reliable source of information.

Adding new action to an existing controller

In your routes

resources :ControllerName do
collection do
get :NewActionName
post :NewActionName
end

end

Make changes like this and it should work

Adding an action to an existing controller (Ruby on Rails)

Your routing isn't set up to allow that route. Assuming you're using the default scaffolding, put this line before the map.resources :posts line in config/routes.rb:

map.connect "posts/:action", :controller => 'posts', :action => /[a-z]+/i

The regex for :action restricts it to just a-z (to avoid catching things like /posts/1). It can be improved if you need underscores or numbers in your new actions.

Create a new action for existing controller

I'm pretty sure you won't be able to do this in a 100% automated way. The reason is that Rails doesn't know what you've done with your routes or controller, and it would require some logic to know how to update these existing files. Your best bet is to just add the new action manually. Add the new method to your controller, update your routes file, and add the view. It will probably take 1 minute at most. Also, if you're not using version controller (which your question eluded to), then you don't have to worry about it automatically overwriting something.

How do I add a new action to a class that inherits from an MVC controller

You can attach the route attribute over DoThis action as shown below

[Route("Home/DoThis")]
[HttpGet]
public IActionResult DoThis()
{
return Json(new
{
Success = true,
Message = "Called DoThis"
});
}

With above now you can access the DoThis action using /Home/DoThis path.

Rails: add an action to a controller

First, create a following route (routes.rb):

get '/users/:id/request' => 'users#request', as: :user_request

Second, add a request method to UsersController:

def request
@user = User.find params[:id]
# do your thing
end

You can create a link to this URL in your views like so: <%= link_to 'Request', user_request_path(@user.id) %>



Related Topics



Leave a reply



Submit