Lost Pets ( Full template + Bootstrap + Simple Form )
Préparation
rails new lost_pet
Mise en place de la DB
rails generate model Pet name address species found_on:date
rails db:migrate
rails generate controler pets
Mise en place de Bootstrap + Simple Form
Bootstrap
yarn add bootstrap
rm app/assets/stylesheets/application.css
touch app/assets/stylesheets/application.scss
code .
Dans application.scss mettre
@import "bootstrap/scss/bootstrap"; /* picks it up in node_modules! */
Simple form
gem 'simple_form', github: 'heartcombo/simple_form'
bundle install
rails generate simple_form:install --bootstrap
First commit
git add .
git commit -m "Rails new with frontend and form gems"
Mise en place des seeds
db/seeds
require 'faker'
Pet.destroy_all
10.times do
Pet.create(
name: Faker::Name.name,
species: ['cat', 'dog', 'warthog', 'duck'].sample)
end
rails db:seed
Configuration des fichiers
controllers/pets_controller.rb
class PetsController < ApplicationController
before_action :set_pet, only: [:show, :edit, :update, :destroy]
def index
@pets = Pet.all
end
def show
end
def new
@pet = Pet.new
end
def create
@pet = Pet.new(pet_params)
if @pet.save
redirect_to pet_path(@pet)
else
render :new
end
end
def destroy
@pet.destroy
redirect_to pets_path
end
def edit
end
def update
@pet.update(pet_params)
redirect_to pet_path(@pet)
end
private
def set_pet
@pet = Pet.find(params[:id])
end
def pet_params
params.require(:pet).permit(:name, :species, :address, :found_on)
end
end
config/routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get '/pets', to: 'pets#index'
get '/pets/new', to: 'pets#new'
post '/pets', to: 'pets#create'
get 'pets/:id/edit', to: 'pets#edit'
patch 'pets/:id', to: 'pets#update'
delete 'pets/:id', to: 'pets#destroy'
get '/pets/:id', to: 'pets#show', as: :pet
end
models/pet.rb
class Pet < ApplicationRecord
SPECIES = ['cat', 'dog', 'warthog', 'duck']
validates :name, presence: { message: 'Your custom error message' }
validates :species, inclusion: { in: SPECIES }
end
views/pet/index.html.erb
<h1> Lost Pets</h1>
<ul>
<% @pets.each do |pet| %>
<li><%= link_to pet.name, pet_path(pet) %> <%= pet.species %></li>
<% end %>
</ul>
views/pet/show.html.erb
<h1> <%= @pet.name %> </h1>
<p> <%= @pet.species %> </p>
<%= link_to "Delete", pet_path(@pet), method: :delete, data: { confirm: "Are you sure ?"} %>
<%= link_to "back", pets_path %>
views/pet/new.html.erb
<br>
<div class="container">
<%= simple_form_for(@pet) do |f| %>
<%= f.input :name, placeholder: "name", label: false %>
<%= f.input :species, collection: Pet::SPECIES.sort %>
<%= f.input :found_on %>
<%= f.input :address %>
<%= f.submit " Soumettre", class: "btn btn-primary" %>
<% end %>
</div>
views/pet/edit.html.erb
<br>
<div class="container">
<%= simple_form_for(@pet) do |f| %>
<%= f.input :name, placeholder: "name", label: false %>
<%= f.input :species, collection: Pet::SPECIES.sort %>
<%= f.input :found_on %>
<%= f.input :address %>
<%= f.submit " Soumettre" %>
<% end %>
</div>