This is an optional exercice for the batch 301 during christmas holidays π
You need to go into your code/$USERNAME
directory in which you will create your rails app.
export USERNAME=replace_this_with_your_github_username
cd ~/code/$USERNAME
During the last lecture you discovered how to handle multi-model apps in Rails.
The goal of this exercice is to make sure your're comfortable with all the complexity induced. To do this, you're going to create an app to create plants associated to a garden, and destroy them.
Here are the user stories:
As a user I can see a list of gardens
As a user I can see the details of one garden
As a user I can fill a form for a new garden
As a user I can create a garden
As a user I can edit an existing garden
As a user I can update a garden
As a user I can destroy a garden
How do you create a rails project?
rails new parks_and_plants
cd parks_and_plants
The best practice is to now create a commit and push your code to Github
git add .
git commit -m "rails new"
hub create
git push origin master
You can check this with:
rails -v
Follow our front-end setup guidelines but don't bother with the Bootstrap JS
part, we won't use any JS here.
You have to generate a garden
model with the CRUD
Each garden has a mandatory name. It has to be unique.
The garden has a banner_url to store a nice picture of it.
Quickly add validations:
# app/models/garden.rb
# [...]
validates :name, presence: true, uniqueness: true
validates :banner_url, presence: true
Seed
You can use this snippet to seed 2 gardens:
# db/seeds.rb
Garden.destroy_all if Rails.env.development?
Garden.create!(
name: "My Little Garden",
banner_url: "https://raw.githubusercontent.com/lewagon/fullstack-images/master/rails/parks-and-plants/garden_1.jpg"
)
Garden.create!(
name: "My Other Garden",
banner_url: "https://raw.githubusercontent.com/lewagon/fullstack-images/master/rails/parks-and-plants/garden_2.jpg"
)
rails db:seed
When your CRUD is complet on gardens you move to the second part.
Here are the user stories:
As a user I can see one garden's plants
As a user I can add a plant in a garden
As a user I can delete a plant
You need to create the second model Plant.
There is a 1:n relation between garden and plants
Don't forget to has the has_many
and belongs_to
in the correct model π
Here is an example of what we want for the garden#show
:
Let's try to add the form to create a plant on the garden show:
Let's now add a user story to make the plants destroyable! Shouldn't be too hard -- you can use this markup for the link:
Happy coding π»