Bonjour mes amis j'ai cette erreur que j'ai été confronté hier sur mon Rails App. Je reçois la méthode undefined `articles 'pour nil: NilClass quand j'ai essayé d'afficher des articles connexes sur ma page show article.undefined la méthode `articles 'pour nil: NilClass quand j'ai essayé d'afficher des articles connexes sur ma page show article
Voici mes codes d'applications
tag.rb
class Tag < ApplicationRecord
has_many :taggings
has_many :articles, through: :taggings
def to_s
name
end
end
tagging.rb
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :article
end
articles_controller
class ArticlesController < ApplicationController
before_action :find_article, only: [:show, :edit, :update, :destroy]
before_action :owned_article, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@articles = Article.all.order("created_at desc")
end
def show
end
def new
@article = current_user.articles.build
end
def create
@article = current_user.articles.build(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def edit
end
def update
if @article.update(article_params)
redirect_to @article, notice: "Your article was successfully updated!"
else
render 'edit'
end
end
def destroy
@article.destroy
redirect_to articles_path
end
private
def find_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :content, :image, :tag_list)
end
def owned_article
unless current_user == @article.user
flash[:alert] = "That article does not belong to you!"
redirect_to root_path
end
end
fin
articles show.html.erb
<div class="container">
<div class="row text-white text-center">
<div class="col-md-10 col-lg-10 ml-sm-auto mr-sm-auto article-show-col">
<br>
<h1><%= @article.title %></h1>
<p class="text-muted">Posted on: <%= @article.created_at.strftime('%-b %-d, %Y') %></p>
<p>
Tags:
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
<!-- <br> -->
<div class="article-show-image">
<%= image_tag @article.image.url(:wide) %>
</div>
<!-- <br> -->
<p><%= @article.content.html_safe %></p>
<hr class="index-hr">
<h5>Broadcast this article</h5>
<%= social_share_button_tag("Hey! Checkout this new article from TWM!") %>
<hr class="index-hr">
**<h5>Related Articles</h5>
<% @tag.articles.each do |article| %>
<li><%= link_to article.title, article_path(article) %></li>
<% end %>**
<div class="btn-group">
<%= link_to "Back", articles_path, class: "btn-custom btn-sm" %>
<% if user_signed_in? %>
<% if @article.user_id == current_user.id %>
<%= link_to "Delete", article_path(@article), method: :delete, data: { confirm: "Are you sure you want to delete this article?" }, class: "btn-custom btn-sm" %>
<%= link_to "Edit", edit_article_path, class: "btn-custom btn-sm" %>
<% end %>
<% end %>
</div>
</div>
</div>
balises contrôleur
class TagsController < ApplicationController
before_action :find_article, only: [:show, :edit, :update, :destroy]
def index
@tags = Tag.all.order("created_at desc")
end
def show
end
def destroy
@tag.destroy
redirect_to tags_path
end
private
def find_article
@tag = Tag.find(params[:id])
end
end
show vi ew pour les balises
<div class="container text-white text-center">
<h1>Articles Tagged with <%= @tag.name %></h1>
<ul>
<% @tag.articles.each do |article| %>
<li><%= link_to article.title, article_path(article) %></li>
<% end %>
</ul>
Merci!
Publiez votre fichier 'routes.rb' et assurez-vous d'inclure la ligne où votre méthode' show' est définie. Il est possible que 'id' ne soit pas le nom de l'attribut que vous devez extraire de' params'. – anothermh
Dans votre article show.html.erb, vous utilisez *@tag.articles et * * @ tag * n'existe pas. –