Giter Site home page Giter Site logo

sinatra-crud-031119ft's Introduction

Sinatra CRUD

What is CRUD?

CRUD stands for Create, Read, Update, Delete. We use this interaction with our apps in order to handle records in our database.

RESTful Routes and CRUD

CRUD and RESTful routes are often confused between the two terminologies.

As CRUD means to create, read, update, and delete. Restful routes consist of 7 routes:

  • Index get '/todos' (Which renders a list of todos)
  • Show get '/todos/:id' (Which renders one specific todo)
  • New get '/todos/new' (Which renders a form to create a todo)
  • Create post '/todos' (Which post data to the controller from the form to create the todo record)
  • Edit get '/todos/:id/edit' (Renders an form to update a specific record)
  • Update patch '/todos/:id' (patch request to the controller to update a todo record from the edit form)
  • Delete delete '/todos/:id' (delete request to the controll to delete a todo record, usuall from a link or button)

Rack MethodOverride Middleware

In order for us to get access to the httpverbs patch and delete you will need to add to the config.ru this line (above the lines where you have use controllername and run application)

use Rack::MethodOverride

Once we have this middleware applied, in our edit or delete forms we will override the method "POST" that we declared in the form tag with a hidden field. An example of this would like something like:

<form action="/todos/<%= @todo.id %>" method="POST">
  <input type="hidden" name="_method" value="patch">
  ... any other form html
</form>

Sinatra by default doesn't have PATCH or DELETE httpverbs. So for us override, we'd use the hidden inputs where the name attribute is set to _method declaring that this input is going to be overriding post, and value which is what we are overriding to.

When Sinatra identifies that param when it's submitted, it'll then match it to the route with that httpverb that's being requested. For example:

# TodosController

patch '/todos/:id' do
  # some logic here
end

sinatra-crud-031119ft's People

Contributors

enoch2k2 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.