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
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
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
owner = Owner.new(first_name: "")
owner.valid?
owner.errors.messages
rails g controller owners index show new create edit update destroy
rails g controller pets index show new create edit update destroy
resources :pets
resources :owners
(rails s) and looking at each path that you created (i.e. /pets)
Hint: scroll to the bottom for an example of standard controller actions
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.
#ruby-on-rails #web-development