{"action":"created","comment":{"url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/comments/47651461","id":47651461,"diff_hunk":"@@ -1,24 +1,101 @@\n # Validations in Controller Actions\n \n-## Objectives\n+Now that we know Rails automatically performs validations defined on models,\n+let's use this information to easily display validation errors to the user.\n \n-  - Define validations on a model\n-  - Use the validation state of a model in a response conditional in an action\n-  - re-render a form with validation errors\n-  - validate a create action\n-  - validate an update action\n+At this point, we'll be covering step two of the following flow:\n \n-## Notes\n+1. User fills out the form and hits \"Submit\", transmitting the form data via\n+   a POST request.\n+2. **The controller sees that validations have failed, and re-renders the\n+   form.**\n+3. The view displays the errors to the user.\n \n-Really simple readme, given a form and a model with validations, what does a controller action that takes this validation into account look like?\n \n-We need to see if the object is valid on form submission, if so, save it, and redirect to the newly created resource.\n+## A Note About Page Loads\n \n-if not, then we want to re-render the form with the invalid instance in scope so that we can use it to introspect on erorrs (more on that very soon, but we need to know what was wrong.) its important to try to remind the student that the @post from Post#new is long gone - every request happens indepedntly, we've recreated the essence of @post from Post#new when the form was submitted with Post.new(params[:post]). We can't redirect to new to reshow the form because then the invalid @post based on params[:post] is gone. Try to explain this idea. Why we render on invalid objects instead of redirecting.\n+You're probably used to validations happening almost instantaneously on websites\n+that you interact with on a daily basis. Every browser has a way of visually\n+indicating that a page load is happening; in Chrome, for example, the circular\n+refresh icon becomes an \"X\" (for \"Stop Loading\"), and reverts to refresh when\n+the load is complete.\n \n-if valid? vs if save on new  - explain the pattern, save returns false and triggers validation\n-if update - same with update\n+A \"page load\" means that everything the browser was doing is thrown out, except\n+for cookies that are stored between requests. When you get validation feedback\n+*without* seeing a page load, that's JavaScript at work, sneakily performing\n+requests in the background without throwing away the current page.\n \n-very common pattern.\n+We won't be using that advanced technique just yet. For now, we'll be performing\n+a full page load every time a form is submitted, which means there will be no\n+validation feedback for the user until they actually hit the \"Submit\" button.\n \n-<a href='https://learn.co/lessons/validations-in-controller-actions-rails' data-visibility='hidden'>View this lesson on Learn.co</a>\n+# Objectives\n+\n+After this lesson, you'll be able to...\n+\n+- Define validations on a model\n+- Use the validation state of a model in a response conditional in an action\n+- Re-render a form with validation errors\n+- Validate a create action\n+- Validate an update action\n+\n+# Manually Checking Validation\n+\n+Up until this point, our `create` action has looked something like this:\n+\n+```ruby\n+# app/controllers/posts_controller.rb\n+\n+  def create\n+    @post = Post.create(post_params)\n+\n+    redirect_to post_path(@post)\n+  end\n+```\n+\n+However, we have two problems now:\n+\n+1. If the post is invalid, there will be no `show` path to redirect to.\n+2. If we redirect, we start a new page load, which will lose all of the\n+   feedback from the validations.\n+\n+The conventional pattern in Rails these days is to test the return value of the\n+CRUD method (like `@post.create` or `@post.save`) and decide what to do based\n+on that. But, for the sake of going slowly and understanding exactly what's\n+going on, we're going to split things up into a few steps before implementing it\n+the \"easy\" way:\n+\n+```ruby\n+# app/controllers/posts_controller.rb\n+\n+  def create\n+    @post = Post.new(post_params)\n+\n+    if @post.valid?\n+      @post.save\n+      redirect_to post_path(@post)\n+    else\n+      render :new\n+    end\n+  end\n+```\n+\n+You can read more about this creative use of `render` in Section 2.2.2 of the\n+Rails Guide on [Layout and Rendering][layout_rendering].\n+\n+[layout_rendering]: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render\n+\n+Remember: **redirects incur a new page load**. If we were to redirect after\n+validation failure, we would **lose** the instance of `@post` that has\n+validation feedback in its `errors` attribute.\n+\n+Another way to differentiate redirects is this:","path":"README.md","position":106,"original_position":106,"commit_id":"42f172799abaee23453ba5993ba732120d890074","original_commit_id":"42f172799abaee23453ba5993ba732120d890074","user":{"login":"loganhasson","id":1240504,"avatar_url":"https://avatars.githubusercontent.com/u/1240504?v=3","gravatar_id":"","url":"https://api.github.com/users/loganhasson","html_url":"https://github.com/loganhasson","followers_url":"https://api.github.com/users/loganhasson/followers","following_url":"https://api.github.com/users/loganhasson/following{/other_user}","gists_url":"https://api.github.com/users/loganhasson/gists{/gist_id}","starred_url":"https://api.github.com/users/loganhasson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/loganhasson/subscriptions","organizations_url":"https://api.github.com/users/loganhasson/orgs","repos_url":"https://api.github.com/users/loganhasson/repos","events_url":"https://api.github.com/users/loganhasson/events{/privacy}","received_events_url":"https://api.github.com/users/loganhasson/received_events","type":"User","site_admin":false},"body":"I like what you're getting at here. Needs a bit more explanation though in the second example to make it clear that this happens if `render` is used in the controller...it's easy to read it quickly and assume it means that refreshes after all form submissions will result in a popup.","created_at":"2015-12-15T15:34:36Z","updated_at":"2015-12-15T15:34:36Z","html_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2#discussion_r47651461","pull_request_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/comments/47651461"},"html":{"href":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2#discussion_r47651461"},"pull_request":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2","id":52495651,"html_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2","diff_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2.diff","patch_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2.patch","issue_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/2","number":2,"state":"open","locked":false,"title":"First pass.","user":{"login":"mcantor","id":14329,"avatar_url":"https://avatars.githubusercontent.com/u/14329?v=3","gravatar_id":"","url":"https://api.github.com/users/mcantor","html_url":"https://github.com/mcantor","followers_url":"https://api.github.com/users/mcantor/followers","following_url":"https://api.github.com/users/mcantor/following{/other_user}","gists_url":"https://api.github.com/users/mcantor/gists{/gist_id}","starred_url":"https://api.github.com/users/mcantor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcantor/subscriptions","organizations_url":"https://api.github.com/users/mcantor/orgs","repos_url":"https://api.github.com/users/mcantor/repos","events_url":"https://api.github.com/users/mcantor/events{/privacy}","received_events_url":"https://api.github.com/users/mcantor/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-03T06:09:17Z","updated_at":"2015-12-15T15:34:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"82bb9ac692a13f0720481ebf405ce2cdd8d11201","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2/commits","review_comments_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2/comments","review_comment_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/comments{/number}","comments_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/2/comments","statuses_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/statuses/42f172799abaee23453ba5993ba732120d890074","head":{"label":"learn-co-curriculum:wip-master","ref":"wip-master","sha":"42f172799abaee23453ba5993ba732120d890074","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":46274392,"name":"validations-in-controller-actions-rails","full_name":"learn-co-curriculum/validations-in-controller-actions-rails","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/validations-in-controller-actions-rails","description":null,"fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails","forks_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/releases{/id}","created_at":"2015-11-16T12:52:14Z","updated_at":"2015-11-16T12:52:14Z","pushed_at":"2015-12-08T01:16:43Z","git_url":"git://github.com/learn-co-curriculum/validations-in-controller-actions-rails.git","ssh_url":"git@github.com:learn-co-curriculum/validations-in-controller-actions-rails.git","clone_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails.git","svn_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails","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":"171e1eb0a854fb117d44299da79687dabc120909","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":46274392,"name":"validations-in-controller-actions-rails","full_name":"learn-co-curriculum/validations-in-controller-actions-rails","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/validations-in-controller-actions-rails","description":null,"fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails","forks_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/releases{/id}","created_at":"2015-11-16T12:52:14Z","updated_at":"2015-11-16T12:52:14Z","pushed_at":"2015-12-08T01:16:43Z","git_url":"git://github.com/learn-co-curriculum/validations-in-controller-actions-rails.git","ssh_url":"git@github.com:learn-co-curriculum/validations-in-controller-actions-rails.git","clone_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails.git","svn_url":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails","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/validations-in-controller-actions-rails/pulls/2"},"html":{"href":"https://github.com/learn-co-curriculum/validations-in-controller-actions-rails/pull/2"},"issue":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/2"},"comments":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/learn-co-curriculum/validations-in-controller-actions-rails/statuses/42f172799abaee23453ba5993ba732120d890074"}}}}