Git Product home page Git Product logo

week-4-day-2-discussion-houston-web-career-021819's Introduction

Forms, CRUD and Active Record Associations

Take 30 minutes and discuss the following with your table group. Try whiteboarding out different solutions and ideas!

  1. Map the CRUD actions (create, read, update, delete) to their matching Sinatra routes and HTTP verbs. For example: In a blog app, displaying the content of one particular blog post corresponds to the "Read" CRUD action and the show RESTful route. The matching route would be an HTTP Get request to '/posts/:id' where the id is that particular post's id i.e. '/posts/6' .

Create -request type:GET -route '/posts/new' #grab the page create page first -view new.erb

 -request type:POST 
 -route '/posts'        #create action happen
 -redirect '/posts'(index.erb)

Read -request type:GET #display all the posts -route '/posts' -view index.erb

 -request type:GET
 -route '/posts/:id'  #display single post
 -view show.erb

Update -request type:GET -route '/posts/:id/edit' #grab/open the edit page for the particular post -view edit.erb

 -request type:PATCH
 -route '/posts/:id'  #it's updating this particular post
 -redirect '/posts/:id' 

Delete -request type:delete -route '/posts/:id' -redirect '/posts'

  1. Build an HTML form to create a new blog post. Then, build a form to edit a post. What are the differences between the two? What routes do they send requests to? What type of requests should each form make?
Create
Title:
<div>
  <label>Content:</label>
  <input type="text" name="post[content]" />
  </div>

   <input type = "submit" id = "Submit"/>
</form>

Edit

Title:
<div>
  <label>Content:</label>
  <input type="text" name="post[content]" value = "<%= post.content %>" />
  </div>

  <input type = "submit" id = "Submit"/>
</form>

Differences: 1. where to update(all the <%= %> parts) Create form route action = "/posts" Edit form route action = "/posts/<%= post.id %>"

Create form request type :POST Edit form request type :PATCH

  1. Revisit your CRUD action-to-route map. What view files, if any, does each action/route correspond to?

Refer to Q1

  1. Write out the Active Record code to preform CRUD actions on a Post model.

Create: @post = Post.create(parmas[:post]) Read: @post = Post.find(params[:id])

Update: 1. @post = Post.find(params[:id]) 2. @post.update(params[:post])

Delete: 1. @post = Post.find(params[:id])
2. @post.delete

  1. Let's think about the domain model for a To-Do list application. Let's say you have a List model and an Item model.
  • What is the relationship between a list and an item?

A list has many items An item belongs to a list

  • A user needs to be able to visit your app, create a new list and then add items to that list. What route would bring the user to the the page to make a new list? What route would take them to the page to make a new item?

    create list route '/lists/new' make new item route '/items/new'

  • How and where would you write the code that associates a new item to its list? In the Model class Item belongs_to :list end

class list has_many :items end

View Forms, CRUD and Active Record Associations on Learn.co and start learning to code for free.

week-4-day-2-discussion-houston-web-career-021819's People

Contributors

alemosie avatar annjohn avatar sophiedebenedetto avatar

Watchers

 avatar

week-4-day-2-discussion-houston-web-career-021819's Issues

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.