This is the 7th video in the Getting Started with Rails 7 series. In this video Mike adds tests and implements the create and new methods for the Article resource, allowing for the creation of an Article record via the application. Following the guide, he first implements create with hardcoded values for the title and body attributes, then implements the new action with the form_with helper. After both of those are created, he uses strong parameters and wires up the form to create Article records dynamically.
This video covers:
- 00:00:10 Introduction
- 00:02:32 Add POST create test to
ArticlesControllerTest - 00:04:14 Add
createaction toArticlesControllerand modify to make the test pass - 00:06:27 Talk through guide explanation of the
newaction instantiating an instance ofArticleandcreateactually saving to the database - 00:08:24 Invoke the
createaction from curl. Note: In the video we temporarily disableverify_authenticity_tokenin our local to demonstrate quickly. DO NOT do this in a real app! - 00:11:21 Redirect vs. render in the
createaction - 00:13:30 Write failing test for
newaction - 00:15:16 Create the
newaction in the controller - 00:16:06 Create blank
new.html.erb. Test now failing instead of erroring - 00:16:50 Review the new form builder and
form_withmethod and add to view template - 00:19:37 Look at the
newaction in the app. Test passing butcreateaction still hardcoded - 00:20:40 The requirement for strong parameters to allow only the attributes you specify
- 00:22:12 Modify the
ArticlesControllerTestto use parameters - 00:23:50 Add article_params private method to
ArticlesController - 00:25:32 Tests are passing. Create works in the application!
- 00:28:16 Commit and push the code
The code for this series is open source and available on GitHub. View the commit for this video here.
