質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Q&A

解決済

1回答

1964閲覧

投稿にタグ付け uninitialized constant Post::TagRelationship

Pro01x19

総合スコア17

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

0グッド

0クリップ

投稿2020/12/14 06:54

前提・実現したいこと

現在SNSのようなアプリケーションを作っていて
投稿にタグ付けをできるように追加実装しています。

https://qiita.com/E6YOteYPzmFGfOD/items/bfffe8c3b31555acd51d

こちらの記事を参考に実装を進めていたのですが、

発生している問題・エラーメッセージ

NameError in Posts#index Showing /Users/mono/projects/rank_top/app/views/posts/index.html.erb where line #47 raised: uninitialized constant Post::TagRelationship Extracted source (around line #47): 45  </div> 46 47  <% post.tags.each do |tag| %> 48   <%= tag.tag_name %>49 50  <% end %>

のようなエラーが出てしまい

中間テーブルやアソシエーションに問題があると仮説を立てて解決しようとしましたが
解決法が分からず実装が止まってしまいました。

該当のソースコード

tag_relationship.rb class TagReletionship < ApplicationRecord belongs_to :post belongs_to :tag end
post.rb class Post < ApplicationRecord has_many :tag_relationship, dependent: :destroy has_many :tags, through: :tag_relationship end
tag.rb class Tag < ApplicationRecord has_many :tag_relationsship, dependent: :destroy has_many :posts, through: :tag_relationship validates :tag_name, uniqueness: true end
routes.rb Rails.application.routes.draw do devise_for :users root to: 'posts#index' resources :users, only: :show resources :posts do resources :comments, only: :create resource :likes, only: [:create, :destroy] collection do get 'search' end end end
posts.index.html.erb <%= form_with url: search_posts_path, method: :get, local: true do |form| %> <%= form.text_field :keyword, placeholder: "投稿を検索する", class: "search-input" %> <%= form.submit "検索", class: "search-btn" %> <% end %> <div class="row row-cols md-3"> <% @posts.each do |post| %> <div class="card d-flex justify-content-around m-5" style="width: auto; heigth: auto;"> <div class="card-body border border-primary"> <%= image_tag post.image, :width => '280', :height => '280' if post.image.attached? %> <h4 class="card-title"><%= post.title %></h4> 1位<p class="card-text"><%= post.rank1 %></p> 2位<p class="card-text"><%= post.rank2 %></p> 3位<p class="card-text"><%= post.rank3 %></p> <div class="btn-group"> <button type="button" class="btn btn-5m btn-outline-secondary"> <%= link_to '詳細', post_path(post.id) , method: :get %></button> <button type="button" class="btn btn-5m btn-outline-secondary"> <%=link_to post.user.name, user_path(post.user.id)%></button> </div> <div id="likes_buttons_<%= post.id %>"> <%= render partial: 'likes/like', locals: { post: post} %> </div> <p class="card-text"> <small class="text-muted"><%= post.updated_at %> </small> </p> </div> </div> <% post.tags.each do |tag| %> <%= tag.tag_name %> <% end %> <% end %> </div>
posts_contoroller.rb class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show, :search] before_action :move_to_index, except: [:index, :show, :search] def index @posts = Post.includes(:user).order('created_at DESC') @likes = Like.where(user_id: current_user) end def new @post = Post.new end def create @post = Post.new(post_params) tag_list = params[:post][:tag_name].split(',') @post.likes_count = (0) if @post.save redirect_to root_path else render :new end end def show @post = Post.find(params[:id]) @comment = Comment.new @comments = @post.comments.includes(:user) end def edit @post = Post.find(params[:id]) @tag_list =@post.tags.pluck(:name).join(",") end def update @post = Post.find(params[:id]) tag_list = params[:post][:tag_ids].split(',') if @post.update(post_params) @post.save_tags(tag_list) redirect_to root_path else render :edit end end def destroy post = Post.find(params[:id]) post.destroy redirect_to root_path end def search @posts =Post.search(params[:keyword]) return nil if params[:input] == "" tag = Tag.where(['tag_name LIKE ?', "%#{params[:input]}%"]) end private def post_params params.require(:post).permit(:title, :rank1, :rank2, :rank3, :image, :likes_count).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index end end end

試したこと

リレーションに問題があると思い、参考記事を元にモデルとマイグレーションファイルを書き直しました。結果、同じエラーが出てしまっています。

備考

現在、タグ付けした投稿を作成、保存、一覧ページの表示を目標に実装しておりますが、
コントローラーのedit,update,searchに今後実装予定の物が含まれています。
質問投稿中に気付き、削除致しましたのでご了承ください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

https://qiita.com/YUD96/items/f751010f8a58efaef6ed
こちらの記事の物を参考に、1から作り直して上手くいきました。

投稿2020/12/14 09:16

Pro01x19

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問