Skip to content

Rails program runs with unexpected results

Sean Lerner edited this page Apr 11, 2017 · 23 revisions

When it comes to Rails, try to follow the flow of your program.

Most http requests follow this flow:

  1. The browser sends the request to the server (if you're issue is happening while developing, then the browser is likely sending the request to IP Address 127.0.0.1 (localhost), TCP Port 3000.

  2. Rails looks up the request in its Routing table

  3. Rails sends the request on to the appropriate Controller

  4. Rails invokes the appropriate Action (aka method) in the Controller

  5. Your Controller Method uses Model(s)

  6. Your Models communicate with the Database

  7. Your Controller Method renders a View and Layout

  8. The response is send back to the browser

How To Fix

Step 1. Identify the HTTP request method being sent:

GET - if the URL was entered into a browser's location bar and enter was pressed, or if a regular link was clicked

POST - if a form submit button was clicked to create a new record

PUT & PATCH - if a form submit button was clicked to update an existing record

DELETE - if a link with the method set to delete was clicked

Step 2. Identify the path of the HTTP request:

Take a look at the URL being sent:

For GET requests: the URL will be in the browser's location bar.

For POST, PUT and PATCH requests: you can see the URL by using debugging tools to inspect the <form> tag. Within the <form> form tag is an action attribute. This will be set to the URL or PATH.

For DELETE requests, inspect the delete link using debugging tools and look at the href attribute.

A URL is made up of several parts:

  • Protocol: http or https
  • Hostname: localhost
  • Port number: 80 or 3000 (sometimes this isn't visible)
  • Path: the segment between the Hostname/Post Number up until either the end of the URL, or until the first ?, whichever comes first. Note the the path can also be empty if nothing follows the Hostname/Post Number
  • Parameters: everything that follows the ? (not necessarily present)

e.g.

  • http://localhost:3000/products/new - the path is products/new
  • http://localhost:3000/ - the path is empty
  • http://localhost:3000/search?q=my+search+terms - the path is search

Step 3. Match the http request method and path with the route

Take a look at your routes using:

rails routes

on the command line.

You should see something like this:

         Prefix Verb   URI Pattern                   Controller#Action
productss_index GET    /productss(.:format)          productss#index
                POST   /productss(.:format)          productss#create
  new_productss GET    /productss/new(.:format)      productss#new
 edit_productss GET    /productss/:id/edit(.:format) productss#edit
      productss GET    /productss/:id(.:format)      productss#show
                PATCH  /productss/:id(.:format)      productss#update
                PUT    /productss/:id(.:format)      productss#update
                DELETE /productss/:id(.:format)      productss#destroy

Clone this wiki locally