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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

2回答

513閲覧

railsでの画像投稿にタグを付けたい

tamtamtime

総合スコア8

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/01/09 02:28

前提

投稿した画像にタグを付けたく、以下を参考に実装しました。
https://qiita.com/MandoNarin/items/5a5610a40c66f77d6c10

実現したいこと

詳細ページでタグを表示させたいです。

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

ただ表示されないだけで、エラーメッセージも出ません。

該当のソースコード

ruby

1class DProductionsController < ApplicationController 2 before_action :set_dproduction, only: [:show, :edit] 3 before_action :authenticate_user!, only:[:edit, :new, :destroy] 4 before_action :move_to_index, except: [:index, :show, :search] 5 6 7 def index 8 @d_production = Dproduction.includes(:user).order("created_at DESC") 9 end 10 11 def new 12 @d_productions = Dproduction.new 13 end 14 15 def create 16 @d_productions = Dproduction.create(d_production_params) 17 if 18 @d_productions.save 19 redirect_to root_path 20 else 21 render :new 22 end 23 end 24 25 def show 26 27 end 28 29 def destroy 30 @d_productions = Dproduction.find(params[:id]) 31 @d_productions.destroy 32 redirect_to root_path 33 end 34 35 def update 36 d_production = Dproduction.find(params[:id]) 37 if 38 d_production.update(d_production_params) 39 redirect_to d_production_path(dproduction.id) 40 else 41 redirect_to request.referer 42 end 43 end 44 45 def edit 46 unless user_signed_in? && current_user.id == @d_production.user_id 47 redirect_to action: :index 48 end 49 end 50 51 def search 52 @d_productions = Dproduction.search(params[:keyword]).order("created_at DESC") 53 end 54 55 private 56 57 def d_production_params 58 params.permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 59 end 60 61 def set_dproduction 62 @d_production = Dproduction.find(params[:id]) 63 end 64 65 def move_to_index 66 unless user_signed_in? 67 redirect_to action: :index 68 end 69 end 70 71 def article_params 72 params.permit(:body, tag_ids: []) 73 end 74 75end

ruby

1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= @d_production.title %> 6 </p> 7 <%# link_to @d_production.user.name, user_path(@d_production.user.id), method: :get, class: :prototype__user %> 8 <% if user_signed_in? && current_user.id == @d_production.user_id %> 9 <div class="prototype__manage"> 10 <%# link_to "編集する", edit_d_production_path, method: :get, class: :prototype__btn %> 11 <%= link_to "削除する", d_production_path, method: :delete, class: :prototype__btn %> 12 </div> 13 <% end %> 14 15 <div class="prototype__detail"><br> 16 <p class="detail__title">ファイル名</p> 17 <p class="detail__message"> 18 <%= @d_production.catch_copy %> 19 </p> 20 </div> 21 22 <div class="prototype__image"> 23 <%= image_tag @d_production.image %> 24 </div> 25 <div class="prototype__body"> 26 27 <div class="prototype__detail"> 28 <p class="detail__title">製作No</p> 29 <p class="detail__message"> 30 <%= @d_production.concept %> 31 </p> 32 <p class="detail__message"> 33 <% @d_production.tags.each do |tag| %> 34 <li> 35 <%= link_to tag.name, tag_path(tag) %> 36 </li> 37 <% end %> 38 </p> 39 </div> 40 </div> 41 42 </div> 43 </div> 44</main>

ruby

1class CreateTags < ActiveRecord::Migration[6.0] 2 def change 3 create_table :tags do |t| 4 t.string :name 5 6 t.timestamps 7 end 8 end 9end

ruby

1class CreateTagRelations < ActiveRecord::Migration[6.0] 2 def change 3 create_table :create_tag_relations do |t| 4 t.references :dproduction, null: false, foreign_key: true 5 t.references :tag, null: false, foreign_key: true 6 7 t.timestamps 8 end 9 end 10end

ruby

1class Tag < ApplicationRecord 2 has_many :create_tag_relations, dependent: :destroy 3 has_many :d_production, through: :create_tag_relations, dependent: :destroy 4end

ruby

1class TagsController < ApplicationController 2 def index 3 @tags = Tag.all 4 end 5 6 def show 7 @tag = Tag.find(params[:id]) 8 end 9 10 def destroy 11 Tag.find(params[:id]).destroy() 12 redirect_to tags_path 13 end 14end

試したこと

別サイトを参考にTagsControllerを追加してみたのですが、やはり表示されません。

補足情報(FW/ツールのバージョンなど)

rails 6.0.0
mysql2 0.5.3

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

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

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

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

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

yuma.inaura

2023/01/09 03:37

既にタグ情報はDBに保存されていてあとは表示の問題だけということなんでしょうか?
tamtamtime

2023/01/09 04:13

seedsに Tag.create([ { name: 'タグ1' }, { name: 'タグ2' }, { name: 'タグ3' }, { name: 'タグ4' }, { name: 'タグ5' } ]) を記載しており、DBに保存はされているのですが、表示だけがされないような状態です。
guest

回答2

0

d_production に関連する tagを each してますが、関連付けがそもそも無いんじゃないでしょうか?

d_production.tags.each

どのviewがどのControllerに対応しているか明記がないのでちゃんと分からないのですが
以下のように @tags を使いたいControllerで変数定義してはいかがですか

def index @d_production = Dproduction.includes(:user).order("created_at DESC") @tags = Tag.all end

投稿2023/01/09 04:33

yuma.inaura

総合スコア1451

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

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

tamtamtime

2023/01/09 04:58

ありがとうございます!追記しても出ませんでしたが、関連付けという観点でもっと調べてみます!
guest

0

ベストアンサー

Tag モデルに登録するだけでは DProduction に Tagは付きません。
「この DProduction には これとこれとこれ のTagをつける」というのをおこないわないと。

View の codeには 関連つけられたTagを表示するようには書かれているようですが、Tagを関連つけるための入力が見当たりません。
また controllerにもない。article_params がそれを連想させますが、そのmethod呼ばれていない ということは Tagが仮にparamsにあったとしても無視される。

投稿2023/01/09 13:27

winterboum

総合スコア23284

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問