【Rails】コメント機能の実装を参考にコメント機能を実装しました。コメント機能は期待通り動いています。
ただ、コメントの表示でコメントした人の名前を呼び出したい時
comment.user.name
とするとnameがnilエラーをはきます。
Userモデルにnameカラムがあることを確認しております。
不明点は
comment.tweet.user.name
とするとエラーはなく名前を呼び出せることです。
原因を教えていただきたいです。
show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>Context:</strong> <%= @tweet.context %> </p> <%= link_to 'Edit', edit_tweet_path(@tweet) %> | <%= link_to 'Back', tweets_path %><br> <h2>comments</h2> <% @comments.each do |comment| %> <%= attachment_image_tag comment.user, :profile_image, :fill, 50, 50, size:'50x50', fallback: "no_image.jpeg", class: "rounded-circle" %> <%= comment.tweet.user.name %> <!------この行です------> <%= comment.content %> <br> <% end %> <h2>reply</h2> <%= form_for([@tweet,@comment]) do |f| %> <%= f.text_area :content %> <%= f.submit %> <% end %>
comments_controller.rb
class CommentsController < ApplicationController before_action :authenticate_user! def create tweet = Tweet.find(params[:tweet_id]) @comment = tweet.comments.build(comment_params) @comment.tweet_id = tweet.id @comment.user_id = current_user.id if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
tweets_controller.rb (中略)
def show @tweet = Tweet.find(params[:id]) @comments = @tweet.comments @comment = @tweet.comments.build end
tweet.rb
ruby
1class Tweet < ApplicationRecord 2 belongs_to :user, dependent: :destroy, optional: true 3 has_many :comments, dependent: :destroy 4end
user.rb
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 validates :name, presence: true 8 validates :introduction, length: { maximum: 200 } 9 10 has_many :tweets, dependent: :destroy 11 has_many :comments, dependent: :destroy 12 13 attachment :profile_image 14end
comment.rb
class Comment < ApplicationRecord belongs_to :user belongs_to :tweet end
schema.rb
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_07_18_091312) do create_table "comments", force: :cascade do |t| t.text "content" t.integer "user_id", null: false t.integer "tweet_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["tweet_id"], name: "index_comments_on_tweet_id" t.index ["user_id"], name: "index_comments_on_user_id" end create_table "tweets", force: :cascade do |t| t.text "context" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "name" t.text "introduction" t.string "profile_image_id" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "comments", "tweets" add_foreign_key "comments", "users" end
検討に必要なファイルがありましたらコメントください。
回答1件
あなたの回答
tips
プレビュー