Skip to content

Instantly share code, notes, and snippets.

@juliends
Last active December 20, 2019 16:43
Show Gist options
  • Save juliends/c508b903c46be7aa8b7eae2f7b164054 to your computer and use it in GitHub Desktop.
Save juliends/c508b903c46be7aa8b7eae2f7b164054 to your computer and use it in GitHub Desktop.

๐ŸŽ„ Garden and Plants ๐ŸŽ„

This is an optional exercice for the batch 301 during christmas holidays ๐ŸŽ…


Preparation

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

Context

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 

Implementation

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

Frontend setup

โš ๏ธ be careful to have the 5.2 version of rails โš ๏ธ

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.

CRUD on gardens

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.

Add the plant model

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:

Garden show

Hard part โš ๏ธ

Let's try to add the form to create a plant on the garden show:

Garden show form

Remove a plant

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 ๐Ÿ’ป

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment