Rails
Rails
seance 1
rails new myApp
in application_controller.rb
in config/routes.rb
Rails.application.routes.draw do
get "up" => "rails/health#show", as: :rails_health_check
in bash
rails db:migrate
# executes the migration in the database, you can see a schema.rb file creating itself
seance 2
creer un controlleur Pages avec 2 actions home et aide (/pages/home and /pages/aide to view them)
les fichiers de tests sont dans test/controllers, to use those tests, il nous faut une bd de test
rails 1
# we add this in test/controllers
test "should get contact" do
get pages_contact_url
assert_response :success
end
# in config/routes.rb, we add:
get 'pages/contact'
# when we run the test again, no error, but there is a failure, there is no contact action in
controller
# in pages_controller.rb, we add
def contact
end
# create views/layouts/_navigation.html.erb
https://getbootstrap.com/docs/3.3/components/#navbar
# copy whatever's in there in this file
# then you can see this in /pages/*, it basically creates the navigation bar for all of the
pages
# in routes.rb
# replace this
get 'pages/home'
get 'pages/aide'
get 'pages/contact'
# with this
root 'pages#home'
get 'aide' => 'pages#aide' # go to pages controller and run aide action
get 'contact' => 'pages#contact'
rails 2
aide_path => '/aide'
aide_url => 'http://localhost:3000/aide'
# with this new config, even if you change the path to contact and aide, the controller will
still be able to find it, contrary to when you hard code it
then we do this
# go to utilisateurs_controller_test.rb
# add this, which will create the user we'll work with
def setup
@utilisateur = Utilisateur.new(nom:"Bugs Bunny", email: "[email protected]")
end
seance 3
rails generate migration add_password_digest_to_utilisateurs password_digest:string
rails db:migrate
seance 4
note to self: look up how to make flash works
# in console
rails generate controller Session new
# in routes.rb, add
get 'login' => 'session#new'
get 'login' => 'session#create'
get 'logout' => 'session#destroy'
rails 3
<%= f.label :password %>
<%= f.password_field :password %>
# go to application_controller.rb, add
class ApplicationController < ActionController::Base
include SessionHelper
end
# so that sessionHelper can be available for all the other pages in the app
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
# go to app/views/layouts/_navigation.html.erb
# replace this
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspo
pup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
rails 4
</ul>
</li>
# with this:
<% if logged_in? %>
<li><%= link_to "Utilisateurs", '#' %></li>
<li class="dropdown">
<a href="#"
class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-haspopup="true"
aria-expanded="false">Compte <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", utilisateur_path(utilisateur_courant) %></li>
<li><%= link_to "Paramètres", '#' %></li>
<li role="separator" class="divider"></li>
<li><%= link_to"Se déconnecter", logout_path, method: "get"%></li>
</ul>
</li>
<% else %>
<li><%= link_to "Se connecter", login_path %></li>
<% end %>
# in app/helpers/session_helper.rb add:
def utilisateur_courant
@utilisateur_courant = @utilisateur_courant || Utilisateur.find_by(id: session[:utilisate
ur_id])
end
def logged_in?
! utilisateur_courant.nil?
end
rails 5
# in session controller add
def update
@utilisateur = Utilisateur.find(params[:id])
if @utilisateur.update(utilisateur_params)
flash[:success] = "profile actualisé!"
redirect_to @utilisateur
else
render 'edit'
end
end
# try to access /utilisateurs/[:id]/edit without being logged in, you'll get an error msg
# create app/views/utilisateurs/index.html.erb
# in the file, write
<h1>Tous les utilisateurs</h1>
<ul class="utilisateurs">
<% @utilisateurs.each do |utilisateur| %>
<li>
<%= link_to utilisateur.nom, utilisateur %>
</li>
<% end %>
rails 6
</ul>
creating an admin user, we’ll add a column to the user table in our db to specify whether it’s an admin or not
# in the cli
rails generate migration add_admin_to_utilisateurs admin:boolean
rails 7
redirect_to(root_url)
end
end
# this ensures it is an admin destroying a user, and if it's normal user, then they're dest
roying their own account
# in index.html.erb
# add under it
# in console
rails generate model Article titre:string description:text utilisateur:references
rails db:migrate
# in models/Article.rb, add
validates :utilisateur_id, presence: true
validates :titre, presence:true, length: {maximum: 200}
validates :description, presence:true, length: {maximum: 20000}
# in consoles
rails generate controllers Articles
rails 8
# in show.html.erb of utilisateurs, add
<% if @utilisateur.articles.any? %>
<h3>Articles (<%= @utilisateur.articles.count %>)</h3>
<ol class="articles">
<%= render @articles %>
</ol>
<% end %>
# go to articles controller
def create
@article = utilisateur_courant.articles.build(article_params)
if @article.save
flash[:success] = "Article crée"
redirect_to root_url
else
redirect_to root_url
end
end
def destroy
end
private
def article_params
params.require(:article).permit(:titre, :description)
end
# we already have the authenticate method in the users controller, if we want to reuse it, w
e'll have to move it to the application controller
now we want to modify home to display the article form if the user is connected, and display the og page if they’re not
connected
<div class="col-sm-8">
<%= f.text_field :titre, rows: 10, class: "form-control", placeholder: "Titre" %>
</div>
<div class="col-sm-8">
<%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Texte"
%>
</div>
<%= f.submit "Soumettre", class: "btn btn-primary" %>
<% end %>
</section>
rails 9
</aside>
</div>
<% else %>
<div class="container">
<div class="jumbotron">
<h1>LE BLOG</h1>
<p>Un blog est un type de site web – ou une partie d'un site web – utilisé pour la publ
ication périodique et régulière d'articles, généralement succincts, et rendant compte d'une a
ctualité autour d'un sujet donné ou d'une profession. (<a href="https://fr.wikipedia.org/wik
i/Blog">Wikipedia</a>)</p>
<!-- <p><a class="btn btn-primary btn-lg" href="#" role="button">S'inscrire</a></p> -->
<%= link_to "S'inscrire", inscription_path, class: "btn btn-lg btn-primary" %>
</div>
</div>
rails 10