15 Simple Steps to Creating a CRUD Web App with Ruby on Rails
If you are fairly new to Ruby on Rails (or forgot how to use it) and need a quick guide to creating a CRUD (create, read, update, destroy) web application, then you’ve come to the right place!
Below I have written some simple steps to creating a very simple application, linked to some documentation or a tutorial for each step, and included an example of what you might write for each step.
Here’s a nice, simple tutorial that gives some more context and explanation and walks you through these steps. This guide is also fairly easy to follow, much longer, and more detailed.
rails new pets-app
2. Launch a server and go to http://localhost:3000 in your browser to view your app
rails s
rails g model owner first_name last_name age:integer email
rails g model pet name species owner_id:integer owner:belongs_to
class Owner < ApplicationRecord
has_many :pets
end
class Owner < ApplicationRecord
has_many :pets
validates :name, presence: true
end
rails db:migrate
7. Check your schema by looking at the db/schema.rb file
Owner.create!(first_name:"Dan", last_name:"Weirdo", age: 18, email:"realemail@cool.com")
Pet.create!(name:"Snowball", species:"dog", owner_id:1)
Owner.first.pets
Pet.first.owner
10. Test validations
owner = Owner.new(first_name: "")
owner.valid?
owner.errors.messages
11. Create controllers and views
rails g controller owners index show new create edit update destroy
rails g controller pets index show new create edit update destroy
12. Create routes using resources or manual routes
resources :pets
resources :owners
13. Make sure your routes are working correctly by running your server
(rails s) and looking at each path that you created (i.e. /pets)
14. Add/edit controller actions
Hint: scroll to the bottom for an example of standard controller actions
15. Add/edit view files
Hint: “find on page” “new.html”, “index.html”, etc. to see examples
Another hint: the form_for helper
That’s it! Make sure you test your code as you go, and commit often.
📝 Read this story later in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.