0
I’m doing a small blog in Ruby on Rails, but I’m having problems in the comments part.
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’m not redirected to the post when I create a comment, but to posts/:post_id/comments
and another problem is that I can’t create comments! I’m simply redirected to posts/:post_id/comments
.
How to solve?
0la. For better code organization, comments_controller in a posts/ folder, so the class name is Posts::Commentscontroller. Even so, I need to put has_many and belongs_to?
– leonero
Need because this is the relationship done in the database. Without it in the model will not work.
– Marco Damaceno