I'm very new to Ruby on Rails and I'm struggling with my scaffolded controller. I made a nested resource where my comments are placed in posts.
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true, :length => { :minimum => 5 }
has_many :comments
end
class Comment < ActiveRecord::Base
validates :commenter, :presence => true
validates :body, :presence => true
belongs_to :post
end
A simplified version of the controller is
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# omited new, index, show...
# POST /comments
# POST /comments.json
def create
post = Post.find(params[:post_id])
@comment = post.comments.create(params[:comment].permit(:name, :title, :context))
respond_to do |format|
if @comment.save
format.html { redirect_to([@comment.post, @comment], notice: 'Comment was successfully created.') }
format.json { render action: 'show', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
post = Post.find(params[:post_id])
@comment = post.comments.find(params[:comment])
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commenter, :body, :post)
end
end
When i filled in the form, i get this exception:
2 errors prohibited this comment from being saved: Commenter can't be blank Body can't be blank
I tried this guide but i think it isn't 100% compatible with Rails 4.
You are reading from params
attributes for a Post
(:name, :title, :context
), but you need to read Comments
attributes (:commenter, :body
)
replace this:
@comment = post.comments.create(params[:comment].permit(:name, :title, :context))
with
@comment = post.comments.create(params[:comment].permit(:commenter, :body))
or, much better, with:
@comment = post.comments.create(comment_params)