I am trying to create my first ever program Using Ruby programming
language on Ruby on Rails and it is a blog! I am following a tutorial
and have created a comments box which does NOT print the comment on the
page so far when written in the comments box, that's the problem. I will
explain step by step I have:
1. Called out on routes.rb:
resources :posts do
resources :comments
end
2. Called out using Cmder:
rails g controller comments create destroy
3. This is my comments_controller.rb class:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment]) #around here is
the error
@comment.save
Firstly, it should be @comment = @post.comments.create instead of build, no point in building something and saving it afterwards.
Secondly, the comment won’t be created because the parameters for the comment are not being sanitized. If rails allowed you to do what you are wanting to do here it would be a terrible security issue.
If you change it to @comment.save! does the response work equally as well or do you get an error.
Try putting a debugger (see pry for Ruby < 2 or byebug for Ruby 2) line on the line before @comment.save, run the code (actually run the test!) and on the debugger line type "@comment.save" and see what it says.