Skip to content

Instantly share code, notes, and snippets.

@erinnachen
Forked from rwarbelow/cfu_crud_in_sinatra.markdown
Last active February 2, 2016 23:05
Show Gist options
  • Save erinnachen/73a62d0970c83683df2c to your computer and use it in GitHub Desktop.
Save erinnachen/73a62d0970c83683df2c to your computer and use it in GitHub Desktop.
CRUD in Sinatra -- Check for Understanding
  1. Define CRUD. CRUD stands for C reate, R ead/Retrieve, U pdate, D elete and these refer to the four functions for working with stored information.

  2. There are seven verb + path combinations that are necessary in a basic Sinatra app in order to provide full CRUD functionality. List each of the seven combinations, and explain what each is for.

  • Verb: GET Path: /index See all the items that are stored (Read)
  • Verb: GET Path: /item See a single item that is stored (Read)
  • Verb: GET Path: /new Load a form in order to create a new item in the database (Create)
  • Verb: POST Path: /index Store the item in the database (Create)
  • Verb: GET Path: /item/update Load a form in order to update the attributes of an item (Update)
  • Verb: PUT Path: /item Update the attributes of an item in the database (Update)
  • Verb: DELETE Path: /item Delete a specific item from the database (Delete)
  1. Why do we use set method_override: true? Form submissions in HTML only accept the methods POST and GET. In order to utilize the verbs PUT(for updating) and DELETE(for deleting), we use set method_override: true in the app and the tags <input type="hidden" name='_method' value='put'> with the form.

  2. Explain the difference between value and name in this line: <input type='text' name='task[title]' value="<%= @task.title %>"/>. name is an internal name so that the input data can be referenced and manipulated. value is what is displayed in the input field; this is what would be written between and opening and closing tag for a double tag, e.g. textarea.

  3. What are params? Where do they come from? params is a hash of data that is generated from the submission of a form. This data comes from the various inputs (text, textarea, etc.) enclosed in the form.

@rwarbelow
Copy link

  1. For these paths, they are generally nested under the plural of whatever object we're referring to. So instead of /new, we would see /tasks/new.
  2. They can also come from the url (for example with the path /tasks/1 and a route of /tasks/:id, we can take 1 out using params[:id]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment