【Ruby on Rails】コメント投稿時user_idが渡らない
解決したいこと
コメント機能を実装しています。
コメント投稿時にuser_idを渡したいですが、はね返されます。
該当するソースコード
Events Controller
class EventsController < ApplicationController
before_action :authenticate_user!
def index
@events = current_user.events
end
def show
@event = Event.find(params[:id])
@comments = @event.comments
@comment = current_user.comments.new
end
def new
@event = Event.new
end
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.save
redirect_to events_path
end
def edit
@event = Event.find(params[:id])
end
def update
@event = Event.find(params[:id])
@event.update
redirect_to events_path
end
def destroy
@event = Event.find(params[:id])
@event.destroy
redirect_to events_path, notice:"削除しました"
end
private
def event_params
params.require(:event).permit(:title, :overview, :start_time, :finish_time, :user_id)
end
end
Comments Controller
class CommentsController < ApplicationController
def create
@comment = Comment.new(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_back(fallback_location: root_path)
else
redirect_back(fallback_location: root_path)
end
end
def destroy
Comment.find_by(id: params[:id], event_id: params[:event_id]).destroy
redirect_back(fallback_location: root_path)
end
private
def comment_params
params.require(:comment).permit(:body, :user_id, :event_id)
end
def event_params
params.require(:event).permit(:title, :overview, :start_time, :finish_time, :user_id)
end
end
Events#show View
--略--
<h2>コメント一覧</h2>
<% @comments.each do |c| %>
<div>
<a href="/users/<%= c.user.id %>"><%= c.user.full_name %></a>
<%= c.body %>
<hr>
</div>
<% end %>
<%= form_with model: @comment, url: event_comments_path(@event), local: true do |f| %>
<%= f.text_field :body %>
<br>
<%= f.submit 'コメントする' %>
<% end %>
User Model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :todos, dependent: :destroy
has_many :titles, dependent: :destroy
has_many :events, dependent: :destroy
has_many :relevant_parties, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :okeys, dependent: :destroy
def already_okeyed?(event)
self.okeys.exists?(event_id: event.id)
end
validates :encrypted_password, presence: true, length: { minimum: 8 }
validates :last_name, presence: true, format: {with: /\A[ぁ-んァ-ン一-龥]/ }
validates :last_name_kana, presence: true, format: {with: /\A[ァ-ヶー-]+\z/ }
validates :first_name, presence: true, format: {with: /\A[ぁ-んァ-ン一-龥]/ }
validates :first_name_kana, presence: true, format: {with: /\A[ァ-ヶー-]+\z/ }
validates :email, presence: true, format: {with: /\A\S+@\S+\.\S+\z/ }
def full_name
self.last_name + self.first_name
end
end
Event Model
class Event < ApplicationRecord
belongs_to :user, dependent: :destroy
has_many :event_todos, dependent: :destroy
has_many :attachments, dependent: :destroy
has_many :relevant_parties, dependent: :destroy
has_many :okeys, dependent: :destroy
has_many :okeyed_users, through: :okeys, source: :user
has_many :comments, dependent: :destroy
end
Comments Migration-file
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.references :user, foreign_key: true
t.references :event, foreign_key: true
t.text :body, null: false
t.timestamps
end
end
end
Events Migration-file
class CreateEvents < ActiveRecord::Migration[5.2]
def change
create_table :events do |t|
t.references :user, foreign_key: true
t.datetime :start_time, null: false
t.datetime :finish_time, null: false
t.string :title, null: false
t.text :overview
t.timestamps
end
end
end
自分で試したこと
byebugでコントローラの記述を試し、アソシエーションやマイグレーションファイルのカラム名にミスがないか確認しました。
かなりの時間詰まってしまっているため、質問させていただきました。
0