Permalink
Browse files

Switch to strong params for blog post creation

We fix the error by using the conventional Rails strong parameters approach of creating a private controller method that specifies which parameters are permitted. Because this is conventionally done in the controller, we don't need to step down to any kind of unit test--letting the acceptance test drive this code is fine.

Outer red: undefined method `title' for nil:NilClass

The next error isn't quite as readable as most we've gotten. What's going on is that the acceptance test attempts to load the last-saved `BlogPost` from the database, but none are saved, so we get `nil` back instead. When we try to check the `title` field, we get an error that there is no `title` method on `nil`. So the actual error is that no blog post is found in the database.
  • Loading branch information...
CodingItWrong committed Feb 26, 2016
1 parent edfcb21 commit a900a09bfa56b9b8e9bdbebc548464bd46941a82
Showing with 7 additions and 1 deletion.
  1. +7 −1 app/controllers/blog_posts_controller.rb
@@ -4,6 +4,12 @@ def new
end
def create
@blog_post = BlogPost.new(params[:blog_post])
@blog_post = BlogPost.new(blog_post_params)
end
private
def blog_post_params
params.require(:blog_post).permit(:title, :body)
end
end

0 comments on commit a900a09

Please sign in to comment.