{"action":"created","comment":{"url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/comments/47796698","id":47796698,"diff_hunk":"@@ -1,33 +1,67 @@\n # Create Action\n \n-## Objectives\n+So we have a form that can accept data and then print out the the values to the screen, that's great for learning how forms work in Rails, however in a real life application we'll most likely want to add the data as a new record in the database. In the `CRUD` lifecycle, this is where the `C` comes in and we `create` a new record.\n \n-1. Use the console to create and persist an AR instance\n-2. Build a functional create action that can use form data in params to persist a corresponding AR instance\n-3. Instantiate an empty instance of a model in create\n-4. Manually assign data to an instance from params\n-5. Save an instance of AR in a controller action\n-6. Understand the missing template error in a create action\n-7. Use redirect_to to redirect in a controller using a route helper (redirect_to post_path(@post))\n+Before implementing this functionality, let's first open up a Rails console session and create a record manually:\n \n-## Notes\n+```\n+post = Post.new\n+post[:title] = \"Title Goes Here\"\n+post[:description] = \"Desc goes here...\"\n+post.save\n+```\n \n-Given a posts#new and a form.html.erb pointing to posts#create and passing data, how do we actually make a post and persist to our database.\n+This syntax will let you manually create a new `Post` record with a `title` and `description` attribute. After running the `save` method in the console you will see output similar to the following:\n \n-we know from console, we can most a post with Post.new and then setting attributes and saving it.\n+```\n+ (0.1ms)  begin transaction\n+SQL (0.3ms)  INSERT INTO \"posts\" (\"title\", \"description\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"title\", \"Title Goes Here\"], [\"description\", \"Desc goes here...\"], [\"created_at\", \"2015-11-23 22:26:43.799742\"], [\"updated_at\", \"2015-11-23 22:26:43.799742\"]]\n+ (1.2ms)  commit transaction\n+=> true\n \n-Let's copy that code to posts#create.\n+```\n \n-Instead of having hard coded data, where is our form data located? params.\n+As you can see, the `save` method generates a SQL script that inserts a new record into the database, passing the `title` and `description` parameters into the statement and then returning the newly created `Post` object. At a high level this is what the `create` method in our controller will be doing.\n \n-Let's use params to manualy assign the post data from the form to the instance of the post and save it.\n+Opening up the `posts_controller.rb` file, let's do a few things to replicate the behavior we had in the console:\n \n-After we save it, lets submit the form and use the console to see if the post was created.\n+1. Create a new `Post` instance\n \n-it was.\n+2. Pass in the parameters from the form\n \n-but we got a rails error. why? because at the end of the posts#create, we left no instructions and rails default took over and tried rendering create.html.erb. What do we want to happen at the end of a create? let's redirect to posts#show for that post\n+3. Save the record\n \n-redirect_to helper method in a controller.\n+To build in this behavior initially you can copy and paste the code that we ran in the console. The only key difference is that now we want to pull in the form data and have that populate the `title` and `description` attributes, you can access each of the form elements by using the hash syntax to grab the elements from the `params` hash that is submitted with the form. The new code in the `create` method should look something like this:\n \n-lets use post_path(@post) to redirect to the newly created post. And we're all good.\n+```ruby\n+def create\n+  post = Post.new\n+  post[:title] = params[:title]\n+  post[:description] = params[:description]\n+  post.save\n+end\n+```\n+\n+If you go to `/posts/new` and fill out the form and submit it, you'll get the error shown below, but this is ok, Rails is simply complaining that we don't have a view template since by default it's trying to render a `create` template that doesn't exist.\n+\n+![Missing Create Template Error](http://reif.io/lib/flatiron/template_error_create.png)\n+\n+If you open up the console you will see that even though we ran into an error page, the record was successfully created in the database, so the form and `create` action are working properly. To fix the error we simply need to redirect the user after they've filled out the form, let's do two refactors:","path":"README.md","position":68,"original_position":68,"commit_id":"e129763dcc8d937968f38894f893614955d93bee","original_commit_id":"e129763dcc8d937968f38894f893614955d93bee","user":{"login":"jmburges","id":58342,"avatar_url":"https://avatars.githubusercontent.com/u/58342?v=3","gravatar_id":"","url":"https://api.github.com/users/jmburges","html_url":"https://github.com/jmburges","followers_url":"https://api.github.com/users/jmburges/followers","following_url":"https://api.github.com/users/jmburges/following{/other_user}","gists_url":"https://api.github.com/users/jmburges/gists{/gist_id}","starred_url":"https://api.github.com/users/jmburges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmburges/subscriptions","organizations_url":"https://api.github.com/users/jmburges/orgs","repos_url":"https://api.github.com/users/jmburges/repos","events_url":"https://api.github.com/users/jmburges/events{/privacy}","received_events_url":"https://api.github.com/users/jmburges/received_events","type":"User","site_admin":false},"body":"Include how to tell that it was written to the DB. Also maybe point out that in the terminal where they did `rails server` that the SQL commands will pass by","created_at":"2015-12-16T16:19:52Z","updated_at":"2015-12-16T16:19:52Z","html_url":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1#discussion_r47796698","pull_request_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/comments/47796698"},"html":{"href":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1#discussion_r47796698"},"pull_request":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1","id":51601001,"html_url":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1","diff_url":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1.diff","patch_url":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1.patch","issue_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/1","number":1,"state":"open","locked":false,"title":"Wip master","user":{"login":"jordanhudgens","id":4793136,"avatar_url":"https://avatars.githubusercontent.com/u/4793136?v=3","gravatar_id":"","url":"https://api.github.com/users/jordanhudgens","html_url":"https://github.com/jordanhudgens","followers_url":"https://api.github.com/users/jordanhudgens/followers","following_url":"https://api.github.com/users/jordanhudgens/following{/other_user}","gists_url":"https://api.github.com/users/jordanhudgens/gists{/gist_id}","starred_url":"https://api.github.com/users/jordanhudgens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jordanhudgens/subscriptions","organizations_url":"https://api.github.com/users/jordanhudgens/orgs","repos_url":"https://api.github.com/users/jordanhudgens/repos","events_url":"https://api.github.com/users/jordanhudgens/events{/privacy}","received_events_url":"https://api.github.com/users/jordanhudgens/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-24T00:23:40Z","updated_at":"2015-12-16T16:19:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e55bcce3ee9b6d7aea1d32d410f941893e019efb","assignee":{"login":"jmburges","id":58342,"avatar_url":"https://avatars.githubusercontent.com/u/58342?v=3","gravatar_id":"","url":"https://api.github.com/users/jmburges","html_url":"https://github.com/jmburges","followers_url":"https://api.github.com/users/jmburges/followers","following_url":"https://api.github.com/users/jmburges/following{/other_user}","gists_url":"https://api.github.com/users/jmburges/gists{/gist_id}","starred_url":"https://api.github.com/users/jmburges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmburges/subscriptions","organizations_url":"https://api.github.com/users/jmburges/orgs","repos_url":"https://api.github.com/users/jmburges/repos","events_url":"https://api.github.com/users/jmburges/events{/privacy}","received_events_url":"https://api.github.com/users/jmburges/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1/commits","review_comments_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1/comments","review_comment_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/comments{/number}","comments_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/1/comments","statuses_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/statuses/e129763dcc8d937968f38894f893614955d93bee","head":{"label":"learn-co-curriculum:wip-master","ref":"wip-master","sha":"e129763dcc8d937968f38894f893614955d93bee","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":46149663,"name":"rails-create-action-readme","full_name":"learn-co-curriculum/rails-create-action-readme","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/rails-create-action-readme","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme","forks_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/releases{/id}","created_at":"2015-11-13T22:00:43Z","updated_at":"2015-11-13T22:05:24Z","pushed_at":"2015-12-02T18:46:44Z","git_url":"git://github.com/learn-co-curriculum/rails-create-action-readme.git","ssh_url":"git@github.com:learn-co-curriculum/rails-create-action-readme.git","clone_url":"https://github.com/learn-co-curriculum/rails-create-action-readme.git","svn_url":"https://github.com/learn-co-curriculum/rails-create-action-readme","homepage":null,"size":5,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"learn-co-curriculum:master","ref":"master","sha":"94c461ae4201bced58897c7f3585b33540d49dcb","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":46149663,"name":"rails-create-action-readme","full_name":"learn-co-curriculum/rails-create-action-readme","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/rails-create-action-readme","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme","forks_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/releases{/id}","created_at":"2015-11-13T22:00:43Z","updated_at":"2015-11-13T22:05:24Z","pushed_at":"2015-12-02T18:46:44Z","git_url":"git://github.com/learn-co-curriculum/rails-create-action-readme.git","ssh_url":"git@github.com:learn-co-curriculum/rails-create-action-readme.git","clone_url":"https://github.com/learn-co-curriculum/rails-create-action-readme.git","svn_url":"https://github.com/learn-co-curriculum/rails-create-action-readme","homepage":null,"size":5,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1"},"html":{"href":"https://github.com/learn-co-curriculum/rails-create-action-readme/pull/1"},"issue":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/1"},"comments":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/learn-co-curriculum/rails-create-action-readme/statuses/e129763dcc8d937968f38894f893614955d93bee"}}}}