Ramy
Ramy

Reputation: 21261

How to decide which action to use

I'm very new to web-development (I feel like all my posts lately have started that way) and becoming, with time, less new to rails. I'm at a point where I can do a sizeable amount of the things required for my job but there's one nagging problem I keep running into:

How do I decide if which action I should use for a given task? index, show, new, edit, create, update or destroy?

destroy is pretty obvious and I can loosely divide the rest into two buckets with index/show in one and new/edit/create in the other. But how do I decide which one to use or if I should build one of my own?

Some general guidelines or links to further reading would be very beneficial for me.

Upvotes: 1

Views: 1086

Answers (2)

robertwbradford
robertwbradford

Reputation: 6595

Here is how I think of these 7 RESTful Controller actions. Take, for example, a Person resource. The corresponding PeopleController would contain the following actions:

index: List a set of people (maybe with some optional conditions).

show: Load a single, previously created Person with the intention of viewing. The corresponding View is usually "read-only."

new: Setup or build an new instance of a Person. It hasn't been saved yet, just setup. The corresponding View is usually some type of form where the user can enter attribute values for this new Person. When this form is submitted, Rails sends it to the "create" action.

create: Save the Person that was setup using the "new" action.

edit: Retrieve a previously created Person with the intention of changing its attributes. The changes have not been made or submitted yet. The corresponding View is usually a form that Rails will submit to the "update" action.

update: Save the changes made when editing a previously created Person.

destroy: Well, as you guessed, destroy or delete a previously created Person.

Of course there is some debate as to whether these 7 actions are sufficient for all controllers, but in my experience they tend to do the job with few exceptions. Adding other actions is usually a sign of needing an additional type of resource.

For example, say you have an HR application full of Person resources you are just dying to hire. In order to accomplish this, you may be tempted to create a "hire" action (i.e., /people/456/hire). However, a more RESTful approach would instead consider this the "creation" of an Employment resource. Something like the following:

class Person < ActiveRecord::Base
  has_many :employments
  has_many :employers, :class_name => 'Company', :through => :employments, :source => :company
end

class Employement < ActiveRecord::Base
  belongs_to :person
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :employments
  has_many :employees, :class_name => 'Person', :through => :employments, :source => :person
end

The EmploymentsController's create action would then be used.

Okay, this is getting long. Don't be afraid to setup a lot of different resources (and you probably won't use all 7 Controller actions for each of these). It pays off in the long run and helps you stick to these 7 basic RESTful actions.

Upvotes: 8

Amadan
Amadan

Reputation: 198324

You can name your actions whatever you want. Generally, by Rails convention, index is the default one, show shows one item, list shows many, new and edit start editing a new or old item, and create and update will save them, respectively. destroy will kill an item, as you guessed. But all these are just conventions: you can name your action yellowtail if that's what you want to do.

Upvotes: 1

Related Questions