I'm doing a small blog in Ruby on Rails, but I'm having problems in the comments section.
In controllers/posts/comments_controller.rb
I have the following:
class Posts::CommentsController < ApplicationController
before_filter :require_authentication
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.student_id = current_student.id
if @comment.save
redirect_to @post
end
end
private
def comment_params
params.require(:comment).permit(:comment)
end
end
In views/posts/_comment.html.erb
I have:
<% if student_signed_in? %>
<%= form_for ([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :comment %>
<%= f.text_area :comment %>
</p>
<p><%= f.submit %></p>
<% end %>
And in views/posts/show.html.erb
I have:
<%= render 'comment' %>
Two things are going wrong: I am not redirected to the post when I create a comment, but for posts/:post_id/comments
and another problem is that I can not create comments! I'm simply redirected to posts/:post_id/comments
.
How to solve?