こんにちは、私はこのエラーを抱えています。記事の表示ページに関連する記事を表示しようとしたときに、は未定義のメソッド `articles 'をnil:NilClassにします。ここで未定義のメソッド `articles 'for nil:記事の表示ページに関連記事を表示しようとしたときのNilClass
は私のアプリのコード
articles_controllertag.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
です
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
エンド
記事は
<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>
タグコントローラ
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
ショーVIをshow.html.erbタグ
<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>
のためのEWはありがとうございました!
'routes.rb'ファイルを投稿し、' show'メソッドが定義されている行を必ず含めてください。 'id'は' params'から引き出す必要のある属性の名前ではない可能性があります。 – anothermh
あなたの記事show.html.erbに、あなたは*@tag.articlesを使用していて、* * @ tag *は存在しません。 –