@post.comments.all is clear. and i dont see any errors after i send form. When i click "Submit" i sent to posts/id/comments, but
my routes.rb
resources :posts do
resources :comments
end
post controller
def show
@current_user ||= User.find(session[:user_id]) if session[:user_id]
@commenter = @current_user
@post = Post.find(params[:id])
@comment = Comment.build_from( @post, @commenter.id, "234234" )
@comments = Comment.all
respond_to do |format|
format.html
format.json { render json: @post }
end
end
comments controller
class CommentsController < ApplicationController
def index
@comments = @post.comments.all
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new params[:comment]
if @comment.save
redirect_to @post # comment not save, so i dont redirect to this page
else
# is that there
end
end
end
post model
acts_as_commentable
has_many :comments
comment model
class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
attr_accessible :commentable, :body, :user_id
validates :body, :presence => true
validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :user
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
:commentable => obj,
:body => comment,
:user_id => user_id
end
#helper method to check if a comment has children
def has_children?
self.children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
end
post view
%h2 Add a comment:
- @comments.each do |c|
= @c.body
= form_for([@post, @comment]) do |f|
.field
= f.label :body
%br/
= f.text_area :body
.actions
= f.submit
Thanks in advance and sorry for bad english
First of all you can debug why @comment.save
return false yourself - just add p @comment.errors
in else block and check server log.
It seems for me that you try to save invalid comments because you don't have setup user
for @comment
in action CommentsController#create
. Comment validates presence of user!
There are several ways how to fix it. Analyzing your code I think the simplest way for you is modify CommentsController#create
#CommentsController
def create
@current_user ||= User.find(session[:user_id]) if session[:user_id]
@post = Post.find(params[:post_id])
@comment = @post.comments.new params[:comment]
@comment.user = @current_user
if @comment.save
redirect_to @post # comment not save, so i dont redirect to this page
else
# is that there
end
end
Another way is to use some gem for authentication - I recommend devise
One more way (very bad way) is to pass user_id through hidden field (you have defined @current_user
in PostsController#show
and user_id in attr_accessible list in Comment). But this is easy way to hack your application and write comments on behalf of any user in system!