I'm trying to create a comment on a video show page. When I submit the form, rails gives me a flash notice: "User must exist, Video must exist". Not sure why my strong params aren't going through to the create method.
comments_controller.rb
def create
@user = current_user
@video = Video.find(params[:video_id])
@comment = Comment.new(comment_params)
@post = @video.post
if @comment.save
flash[:notice] = "Comment successfully created"
redirect_to post_video_path(@post, @video)
else
@errors = @comment.errors.full_messages.join(', ')
flash[:notice] = @errors
render :'videos/show'
end
private
def comment_params
params.require(:comment).permit(
:body,
:user,
:video
)
end
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :video
validates :body, presence: true
end
models/video.rb
class Video < ActiveRecord::Base
belongs_to :user
belongs_to :post
has_many :comments
end
views/videos/show.html.erb
<%= @video.title %>
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{@video.embed_id}") %>
<%= link_to "Delete Video", post_video_path(@post, @video), method: :delete %>
<%= link_to('Back', user_post_path(@user, @post)) %>
<h3>Comments</h3>
<%= form_for [@video, @comment] do |f| %>
<%= f.label(:body, "Comment") %>
<%= f.text_area(:body) %>
<%= f.submit("Submit Comment") %>
<% end %>
<% unless @comments.nil? %>
<% @comments.each do |comment| %>
<%= comment.body %>
<%= comment.user %>
<% end %>
<% end %>
I tried adding this to the create method...
@comment.user = current_user
@comment.video = @video
That allowed the comment to save but instead of displaying the comment.body, it displayed the comment object. It still doesn't explain why the strong params aren't being passed.