{"action":"created","comment":{"url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/comments/79969713","id":79969713,"diff_hunk":"@@ -0,0 +1,174 @@\n+---\n+title: CRUD in Sinatra Redux\n+length: 120\n+tags: crud, sinatra\n+---\n+\n+### Goals\n+\n+By the end of this lesson, you will know/be able to:\n+\n+* define CRUD in the programming sense of the word\n+* add functionality to complete the *U*pdate and *D*elete functionality of CRUD for a Sinatra app\n+* divide responsibilities between the controller, views, and models\n+\n+### Structure\n+\n+* Lecture\n+* Code-along\n+* Work Time\n+\n+### Repository\n+\n+* If you have been following the [TaskManager](https://github.com/s-espinosa/task_manager_redux) tutorials up to this point, you should be able to use the version you already have.\n+* If not, clone the [TaskManager Repo](https://github.com/s-espinosa/task_manager_redux), and switch to the crud-lesson branch.\n+\n+### Warm Up\n+\n+* What does CRUD stand for and why is it important?\n+* What does MVC stand for? Create a diagram of the MVC pattern.\n+* What part of the MVC does `task_manager_app.rb` represent in our current TaskManager app? What about `task.rb`?\n+\n+### CRUD Introduction\n+\n+Assuming we want to create full CRUD functionality in our Sinatra app for users accessing our site through a browser, there are seven routes that we will need to define. Let's make a chart together.\n+\n+(Here's a [completed chart](https://www.dropbox.com/s/vx3ocfsusjdrgfw/crud_in_sinatra.pdf?dl=0), but don't open it until you're finished!)\n+\n+### Code-Along\n+\n+We're going to follow the MVC design pattern (Rails uses this by default, but in Sinatra we will need to create this structure ourselves).\n+\n+So far, we are able to create a task and read a task. How do we add functionality to update and delete tasks?\n+\n+#### Editing a task\n+\n+Our **edit** route should bring the user to a form where they can change the title and description of the task. In order to create this functionality, we'll need to create a button to get to the edit page, a route in our controller for that link, and a view that will be rendered when we hit the new route.\n+\n+In our `index.erb` view:\n+\n+```erb\n+<h1>All Tasks</h1>\n+\n+<% @tasks.each do |task| %>\n+  <h3><a href=\"/tasks/<%= task.id %>\"><%= task.title %></a></h3>\n+  <p><%= task.description  %></p>\n+  <a href=\"/tasks/<%= task.id %>/edit\">Edit</a>\n+<% end %>\n+```\n+\n+In our controller:\n+\n+```ruby\n+  get '/tasks/:id/edit' do\n+    @task = Task.find(params[:id])\n+    erb :edit\n+  end\n+```\n+In our view, `edit.erb`:\n+\n+```erb\n+<form action=\"/tasks/<%= @task.id %>\" method=\"post\">\n+  <p>Edit</p>\n+  <input type=\"hidden\" name=\"_method\" value=\"PUT\" />\n+  <input type='text' name='task[title]' value=\"<%= @task.title %>\"/><br/>\n+  <textarea name='task[description]'><%= @task.description %></textarea><br/>\n+  <input type='submit'/>\n+</form>\n+```\n+\n+#### Updating a task\n+\n+Our new form needs somewhere to go when a user clicks submit. We'll use the **update** route to do the work of changing the task in the database.\n+\n+In our controller:\n+\n+```ruby\n+  set :method_override, true  # this allows us to use _method in the form\n+  ...\n+  put '/tasks/:id' do |id|\n+    Task.update(id.to_i, params[:task])\n+    redirect \"/tasks/#{id}\"\n+  end\n+```\n+\n+In our TaskManager model:\n+\n+```ruby\n+  def self.update(id, task_params)\n+    database = SQLite3::Database.new('db/task_manager_development.db')\n+    database.results_as_hash = true","path":"ruby_02-web_applications_with_ruby/outlines/intro_to_crud_redux.markdown","position":100,"original_position":100,"commit_id":"de6810d97c4b87ebcf91b9f985ac2bc0a7f8bb0d","original_commit_id":"de6810d97c4b87ebcf91b9f985ac2bc0a7f8bb0d","user":{"login":"case-eee","id":6503717,"avatar_url":"https://avatars.githubusercontent.com/u/6503717?v=3","gravatar_id":"","url":"https://api.github.com/users/case-eee","html_url":"https://github.com/case-eee","followers_url":"https://api.github.com/users/case-eee/followers","following_url":"https://api.github.com/users/case-eee/following{/other_user}","gists_url":"https://api.github.com/users/case-eee/gists{/gist_id}","starred_url":"https://api.github.com/users/case-eee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/case-eee/subscriptions","organizations_url":"https://api.github.com/users/case-eee/orgs","repos_url":"https://api.github.com/users/case-eee/repos","events_url":"https://api.github.com/users/case-eee/events{/privacy}","received_events_url":"https://api.github.com/users/case-eee/received_events","type":"User","site_admin":false},"body":"This, along with line 99 have been taken care of in `#initialize`, right? So we could just reference `@database` on line 101?","created_at":"2016-09-22T02:49:20Z","updated_at":"2016-09-22T02:49:21Z","html_url":"https://github.com/turingschool/lesson_plans/pull/251#discussion_r79969713","pull_request_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251","_links":{"self":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/comments/79969713"},"html":{"href":"https://github.com/turingschool/lesson_plans/pull/251#discussion_r79969713"},"pull_request":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251"}}},"pull_request":{"url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251","id":85909603,"html_url":"https://github.com/turingschool/lesson_plans/pull/251","diff_url":"https://github.com/turingschool/lesson_plans/pull/251.diff","patch_url":"https://github.com/turingschool/lesson_plans/pull/251.patch","issue_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/251","number":251,"state":"open","locked":false,"title":"[WIP] Draft updated intro to CRUD lesson","user":{"login":"s-espinosa","id":10855116,"avatar_url":"https://avatars.githubusercontent.com/u/10855116?v=3","gravatar_id":"","url":"https://api.github.com/users/s-espinosa","html_url":"https://github.com/s-espinosa","followers_url":"https://api.github.com/users/s-espinosa/followers","following_url":"https://api.github.com/users/s-espinosa/following{/other_user}","gists_url":"https://api.github.com/users/s-espinosa/gists{/gist_id}","starred_url":"https://api.github.com/users/s-espinosa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/s-espinosa/subscriptions","organizations_url":"https://api.github.com/users/s-espinosa/orgs","repos_url":"https://api.github.com/users/s-espinosa/repos","events_url":"https://api.github.com/users/s-espinosa/events{/privacy}","received_events_url":"https://api.github.com/users/s-espinosa/received_events","type":"User","site_admin":false},"body":"@case-eee Updated CRUD lesson to go with the revised Task Master for discussion.","created_at":"2016-09-20T04:33:15Z","updated_at":"2016-09-22T02:49:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a39f061197ba163f70a6067b66de4ed9a737cd54","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251/commits","review_comments_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251/comments","review_comment_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls/comments{/number}","comments_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/251/comments","statuses_url":"https://api.github.com/repos/turingschool/lesson_plans/statuses/de6810d97c4b87ebcf91b9f985ac2bc0a7f8bb0d","head":{"label":"turingschool:update_crud","ref":"update_crud","sha":"de6810d97c4b87ebcf91b9f985ac2bc0a7f8bb0d","user":{"login":"turingschool","id":7934292,"avatar_url":"https://avatars.githubusercontent.com/u/7934292?v=3","gravatar_id":"","url":"https://api.github.com/users/turingschool","html_url":"https://github.com/turingschool","followers_url":"https://api.github.com/users/turingschool/followers","following_url":"https://api.github.com/users/turingschool/following{/other_user}","gists_url":"https://api.github.com/users/turingschool/gists{/gist_id}","starred_url":"https://api.github.com/users/turingschool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/turingschool/subscriptions","organizations_url":"https://api.github.com/users/turingschool/orgs","repos_url":"https://api.github.com/users/turingschool/repos","events_url":"https://api.github.com/users/turingschool/events{/privacy}","received_events_url":"https://api.github.com/users/turingschool/received_events","type":"Organization","site_admin":false},"repo":{"id":23230424,"name":"lesson_plans","full_name":"turingschool/lesson_plans","owner":{"login":"turingschool","id":7934292,"avatar_url":"https://avatars.githubusercontent.com/u/7934292?v=3","gravatar_id":"","url":"https://api.github.com/users/turingschool","html_url":"https://github.com/turingschool","followers_url":"https://api.github.com/users/turingschool/followers","following_url":"https://api.github.com/users/turingschool/following{/other_user}","gists_url":"https://api.github.com/users/turingschool/gists{/gist_id}","starred_url":"https://api.github.com/users/turingschool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/turingschool/subscriptions","organizations_url":"https://api.github.com/users/turingschool/orgs","repos_url":"https://api.github.com/users/turingschool/repos","events_url":"https://api.github.com/users/turingschool/events{/privacy}","received_events_url":"https://api.github.com/users/turingschool/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/turingschool/lesson_plans","description":"Lesson Plans for the Turing School of Software & Design","fork":false,"url":"https://api.github.com/repos/turingschool/lesson_plans","forks_url":"https://api.github.com/repos/turingschool/lesson_plans/forks","keys_url":"https://api.github.com/repos/turingschool/lesson_plans/keys{/key_id}","collaborators_url":"https://api.github.com/repos/turingschool/lesson_plans/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/turingschool/lesson_plans/teams","hooks_url":"https://api.github.com/repos/turingschool/lesson_plans/hooks","issue_events_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/events{/number}","events_url":"https://api.github.com/repos/turingschool/lesson_plans/events","assignees_url":"https://api.github.com/repos/turingschool/lesson_plans/assignees{/user}","branches_url":"https://api.github.com/repos/turingschool/lesson_plans/branches{/branch}","tags_url":"https://api.github.com/repos/turingschool/lesson_plans/tags","blobs_url":"https://api.github.com/repos/turingschool/lesson_plans/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/turingschool/lesson_plans/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/turingschool/lesson_plans/git/refs{/sha}","trees_url":"https://api.github.com/repos/turingschool/lesson_plans/git/trees{/sha}","statuses_url":"https://api.github.com/repos/turingschool/lesson_plans/statuses/{sha}","languages_url":"https://api.github.com/repos/turingschool/lesson_plans/languages","stargazers_url":"https://api.github.com/repos/turingschool/lesson_plans/stargazers","contributors_url":"https://api.github.com/repos/turingschool/lesson_plans/contributors","subscribers_url":"https://api.github.com/repos/turingschool/lesson_plans/subscribers","subscription_url":"https://api.github.com/repos/turingschool/lesson_plans/subscription","commits_url":"https://api.github.com/repos/turingschool/lesson_plans/commits{/sha}","git_commits_url":"https://api.github.com/repos/turingschool/lesson_plans/git/commits{/sha}","comments_url":"https://api.github.com/repos/turingschool/lesson_plans/comments{/number}","issue_comment_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/comments{/number}","contents_url":"https://api.github.com/repos/turingschool/lesson_plans/contents/{+path}","compare_url":"https://api.github.com/repos/turingschool/lesson_plans/compare/{base}...{head}","merges_url":"https://api.github.com/repos/turingschool/lesson_plans/merges","archive_url":"https://api.github.com/repos/turingschool/lesson_plans/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/turingschool/lesson_plans/downloads","issues_url":"https://api.github.com/repos/turingschool/lesson_plans/issues{/number}","pulls_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls{/number}","milestones_url":"https://api.github.com/repos/turingschool/lesson_plans/milestones{/number}","notifications_url":"https://api.github.com/repos/turingschool/lesson_plans/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/turingschool/lesson_plans/labels{/name}","releases_url":"https://api.github.com/repos/turingschool/lesson_plans/releases{/id}","deployments_url":"https://api.github.com/repos/turingschool/lesson_plans/deployments","created_at":"2014-08-22T15:39:18Z","updated_at":"2016-09-16T21:28:47Z","pushed_at":"2016-09-22T00:56:10Z","git_url":"git://github.com/turingschool/lesson_plans.git","ssh_url":"git@github.com:turingschool/lesson_plans.git","clone_url":"https://github.com/turingschool/lesson_plans.git","svn_url":"https://github.com/turingschool/lesson_plans","homepage":null,"size":6449,"stargazers_count":106,"watchers_count":106,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":217,"mirror_url":null,"open_issues_count":33,"forks":217,"open_issues":33,"watchers":106,"default_branch":"master"}},"base":{"label":"turingschool:master","ref":"master","sha":"2302a4d443f08bff08e87081ca8dd630b1113469","user":{"login":"turingschool","id":7934292,"avatar_url":"https://avatars.githubusercontent.com/u/7934292?v=3","gravatar_id":"","url":"https://api.github.com/users/turingschool","html_url":"https://github.com/turingschool","followers_url":"https://api.github.com/users/turingschool/followers","following_url":"https://api.github.com/users/turingschool/following{/other_user}","gists_url":"https://api.github.com/users/turingschool/gists{/gist_id}","starred_url":"https://api.github.com/users/turingschool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/turingschool/subscriptions","organizations_url":"https://api.github.com/users/turingschool/orgs","repos_url":"https://api.github.com/users/turingschool/repos","events_url":"https://api.github.com/users/turingschool/events{/privacy}","received_events_url":"https://api.github.com/users/turingschool/received_events","type":"Organization","site_admin":false},"repo":{"id":23230424,"name":"lesson_plans","full_name":"turingschool/lesson_plans","owner":{"login":"turingschool","id":7934292,"avatar_url":"https://avatars.githubusercontent.com/u/7934292?v=3","gravatar_id":"","url":"https://api.github.com/users/turingschool","html_url":"https://github.com/turingschool","followers_url":"https://api.github.com/users/turingschool/followers","following_url":"https://api.github.com/users/turingschool/following{/other_user}","gists_url":"https://api.github.com/users/turingschool/gists{/gist_id}","starred_url":"https://api.github.com/users/turingschool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/turingschool/subscriptions","organizations_url":"https://api.github.com/users/turingschool/orgs","repos_url":"https://api.github.com/users/turingschool/repos","events_url":"https://api.github.com/users/turingschool/events{/privacy}","received_events_url":"https://api.github.com/users/turingschool/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/turingschool/lesson_plans","description":"Lesson Plans for the Turing School of Software & Design","fork":false,"url":"https://api.github.com/repos/turingschool/lesson_plans","forks_url":"https://api.github.com/repos/turingschool/lesson_plans/forks","keys_url":"https://api.github.com/repos/turingschool/lesson_plans/keys{/key_id}","collaborators_url":"https://api.github.com/repos/turingschool/lesson_plans/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/turingschool/lesson_plans/teams","hooks_url":"https://api.github.com/repos/turingschool/lesson_plans/hooks","issue_events_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/events{/number}","events_url":"https://api.github.com/repos/turingschool/lesson_plans/events","assignees_url":"https://api.github.com/repos/turingschool/lesson_plans/assignees{/user}","branches_url":"https://api.github.com/repos/turingschool/lesson_plans/branches{/branch}","tags_url":"https://api.github.com/repos/turingschool/lesson_plans/tags","blobs_url":"https://api.github.com/repos/turingschool/lesson_plans/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/turingschool/lesson_plans/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/turingschool/lesson_plans/git/refs{/sha}","trees_url":"https://api.github.com/repos/turingschool/lesson_plans/git/trees{/sha}","statuses_url":"https://api.github.com/repos/turingschool/lesson_plans/statuses/{sha}","languages_url":"https://api.github.com/repos/turingschool/lesson_plans/languages","stargazers_url":"https://api.github.com/repos/turingschool/lesson_plans/stargazers","contributors_url":"https://api.github.com/repos/turingschool/lesson_plans/contributors","subscribers_url":"https://api.github.com/repos/turingschool/lesson_plans/subscribers","subscription_url":"https://api.github.com/repos/turingschool/lesson_plans/subscription","commits_url":"https://api.github.com/repos/turingschool/lesson_plans/commits{/sha}","git_commits_url":"https://api.github.com/repos/turingschool/lesson_plans/git/commits{/sha}","comments_url":"https://api.github.com/repos/turingschool/lesson_plans/comments{/number}","issue_comment_url":"https://api.github.com/repos/turingschool/lesson_plans/issues/comments{/number}","contents_url":"https://api.github.com/repos/turingschool/lesson_plans/contents/{+path}","compare_url":"https://api.github.com/repos/turingschool/lesson_plans/compare/{base}...{head}","merges_url":"https://api.github.com/repos/turingschool/lesson_plans/merges","archive_url":"https://api.github.com/repos/turingschool/lesson_plans/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/turingschool/lesson_plans/downloads","issues_url":"https://api.github.com/repos/turingschool/lesson_plans/issues{/number}","pulls_url":"https://api.github.com/repos/turingschool/lesson_plans/pulls{/number}","milestones_url":"https://api.github.com/repos/turingschool/lesson_plans/milestones{/number}","notifications_url":"https://api.github.com/repos/turingschool/lesson_plans/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/turingschool/lesson_plans/labels{/name}","releases_url":"https://api.github.com/repos/turingschool/lesson_plans/releases{/id}","deployments_url":"https://api.github.com/repos/turingschool/lesson_plans/deployments","created_at":"2014-08-22T15:39:18Z","updated_at":"2016-09-16T21:28:47Z","pushed_at":"2016-09-22T00:56:10Z","git_url":"git://github.com/turingschool/lesson_plans.git","ssh_url":"git@github.com:turingschool/lesson_plans.git","clone_url":"https://github.com/turingschool/lesson_plans.git","svn_url":"https://github.com/turingschool/lesson_plans","homepage":null,"size":6449,"stargazers_count":106,"watchers_count":106,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":217,"mirror_url":null,"open_issues_count":33,"forks":217,"open_issues":33,"watchers":106,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251"},"html":{"href":"https://github.com/turingschool/lesson_plans/pull/251"},"issue":{"href":"https://api.github.com/repos/turingschool/lesson_plans/issues/251"},"comments":{"href":"https://api.github.com/repos/turingschool/lesson_plans/issues/251/comments"},"review_comments":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251/comments"},"review_comment":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/turingschool/lesson_plans/pulls/251/commits"},"statuses":{"href":"https://api.github.com/repos/turingschool/lesson_plans/statuses/de6810d97c4b87ebcf91b9f985ac2bc0a7f8bb0d"}}}}
{"actor":{"display_login":"case-eee"}}