Entrée des données initiales
db/seeds.rb
Article.create(user_id: 1, content: 'hello world')
Article.create(user_id: 1, content: 'hello paiza')
Article.create(user_id: 2, content: 'Monde de Hello, tout le monde')
Reflété dans la base de données
$ rails db:seed
Autoriser uniquement des actions spécifiques à effectuer lors de la connexion
articles_controller.rb
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :set_article, only: [:show, :edit, :update, :destroy]
Modèle d'association d'articles et modèle d'utilisateur
model/article.rb
class Article < ApplicationRecord
belongs_to :user
end
Afficher l'adresse e-mail de l'affiche
views/articles/index.html.erb
<table>
<thead>
<tr>
<th>User</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= article.user.email %></td>
<td><%= article.content %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
Afficher la navigation en commun
app/views/layouts/application.html.erb
<body>
<% if user_signed_in? %>
Logged in as <strong><%= current_user.email %></strong>.
<%= link_to "Settings", edit_user_registration_path %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
<% end %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
Ajouter une colonne au modèle utilisateur
string
$ rails db:migrate
Ajout de la colonne "nom" à l'écran d'inscription
app/views/devise/registrations/new.html.erb
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
Ajout de la colonne "nom" à l'écran de modification des informations utilisateur
app/views/devise/registrations/edit.html.erb
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
Enregistrez la colonne de nom dans le contrôleur
app/controllers/application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
Afficher le nom d'utilisateur dans les informations de connexion de navigation
app/views/layouts/application.html.erb
<% if user_signed_in? %>
Logged in as <strong><%= current_user.name %></strong>.
<%= link_to "Settings", edit_user_registration_path, :class => "navbar-link" %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => "navbar-link" %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
<% end %>
Afficher la colonne du nom dans la liste des articles
views/articles/index.erb.html
<table>
<thead>
<tr>
<th>Name</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= article.user.name %></td>
<td><%= article.content %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
Afficher la colonne du nom sur l'écran des détails de l'article
views/articles/show.erb.html
<p>
<strong>User:</strong>
<%= @article.user.name %>
</p>
Modifier le nouveau formulaire de publication et supprimer user_id
app/views/articles/_form.html.erb
<%= form_with(model: article, local: true) do |form| %>
<% if article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :content %>
<%= form.text_field :content, id: :article_content %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Modifier la méthode de création
app/controllers/articles_controller.rb
# POST /articles
# POST /articles.json
def create
@article = Article.new(article_params)
@article.user_id = current_user.id
Modifier la méthode de mise à jour
app/controllers/articles_controller.rb
def update
if @article.user_id == current_user.id
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
else
redirect_to @article, notice: "You don't have permission."
end
end
Modifier la méthode de destruction
app/controllers/articles_controller.rb
def destroy
if @article.user_id == current_user.id
@article.destroy
msg = "Article was successfully destroyed."
else
msg = "You don't have permission."
end
respond_to do |format|
format.html { redirect_to articles_url, notice: msg }
format.json { head :no_content }
end
end
Correction de l'action de mise à jour
app/controllers/articles_controller.rb
def update
if @article.user_id == current_user.id
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
else
redirect_to @article, notice: "You don't have permission."
end
end
Modifier l'action de destruction
app/controllers/articles_controller.rb
def destroy
if @article.user_id == current_user.id
@article.destroy
msg = "Article was successfully destroyed."
else
msg = "You don't have permission."
end
respond_to do |format|
format.html { redirect_to articles_url, notice: msg }
format.json { head :no_content }
end
end
Afficher la liste des articles
app/views/articles/index.html.erb
<% if user_signed_in? && article.user_id == current_user.id %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
Modifier l'écran des détails
app/views/articles/show.html.erb
<% if user_signed_in? && @article.user_id == current_user.id %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<% end %>
<%= link_to 'Back', articles_path %>
Recommended Posts