Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how I think of these 7 RESTful <a href="http://en.wikipedia.org/wiki/Model-View-Controller" rel="noreferrer">Controller</a> actions. Take, for example, a Person resource. The corresponding PeopleController would contain the following actions:</p> <p><strong>index</strong>: List a set of people (maybe with some optional conditions).</p> <p><strong>show</strong>: Load a single, previously created Person <em>with the intention of viewing</em>. The corresponding View is usually "read-only."</p> <p><strong>new</strong>: 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.</p> <p><strong>create</strong>: Save the Person that was setup using the "new" action.</p> <p><strong>edit</strong>: Retrieve a previously created Person <em>with the intention of changing its attributes</em>. The changes have not been made or submitted yet. The corresponding View is usually a form that Rails will submit to the "update" action.</p> <p><strong>update</strong>: Save the changes made when editing a previously created Person.</p> <p><strong>destroy</strong>: Well, as you guessed, destroy or delete a previously created Person.</p> <p>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.</p> <p>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:</p> <pre><code>class Person &lt; ActiveRecord::Base has_many :employments has_many :employers, :class_name =&gt; 'Company', :through =&gt; :employments, :source =&gt; :company end class Employement &lt; ActiveRecord::Base belongs_to :person belongs_to :company end class Company &lt; ActiveRecord::Base has_many :employments has_many :employees, :class_name =&gt; 'Person', :through =&gt; :employments, :source =&gt; :person end </code></pre> <p>The EmploymentsController's create action would then be used.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload