user1312490
user1312490

Reputation: 171

Create my own action in rails

Is there a way to create my own action?

For example when i use scaffold, in the controller it creates "index", "show", "edit" , "new", "create", "update" and "destroy" actions.

I want to add another action.

I have read that i need to add some code on routes file...

Can anyone help me?

Upvotes: 0

Views: 547

Answers (2)

James I
James I

Reputation: 198

You can generate your own actions in a controller simply by defining a method:

def action
  # do something here
end

You'll need to make sure you have a route setup in config/routes.rb, too.

For more information, I'd suggest reading the guide at http://guides.rubyonrails.org/getting_started.html, specifically http://guides.rubyonrails.org/getting_started.html#generating-a-controller. You'll find that although scaffolds can be useful initially, you have to write your own controller files / action methods as soon as your application gets more complicated.

Upvotes: 4

Viren
Viren

Reputation: 5962

Well you can just define the action routing in your routes and way you go

Hope this help

Upvotes: 0

Related Questions