前提
投稿した画像にタグを付けたく、以下を参考に実装しました。
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
回答2件