I just got a new puppy, so I was inspired to create the Pupdate Blog to create posts about my puppy's training progress. However, anyone can sign up, log in, and create their own Pupdates about their own dog.
First, I used the corneal-new gem to create boilerplate code. I added views, models, and controllers.
Users sign up using an email and password. I used the bcrypt gem to ensure the user's password is protected.
Then I set up the UsersController and PupdatesController files and mounted them in config.ru. I also added "user Rack::MethodOverride" to give access to Rack middleware so my app will have access to HTTP verbs other than GET and POST, such as PATCH and DELETE.
I created two migrations with rake db:create_migration: one for the users table and one for the pupdates table. The users table has 3 columns: name, email, password_digest. The pupdates table has a title, content, and a user_id.
Next, I created a seed file to fill the table with some dummy data to play with and test the app's functionality.
Back in the controllers, I created the routes needed for the UsersController to sign up, log in, and log out. I also added in the PupdatesController the ability to create a new post, edit a post, and delete a post. I also authenticated the user to ensure that a user can't edit or delete other users' posts. Helper methods were useful for that!
Since all of the functions are behaving well, I added some UX features. I used the flash gem to flash messages at users to give them information. For example, when you delete a post, "Pupdate deleted" appears on the screen. Users also see a flash message when they enter invalid login credentials.
Top comments (0)