Я следовал руководству, чтобы создать приложение для блога. Так что у меня есть посты и комментарии. Проверка для полей в форме сообщений работает отлично. Проверка в разделе комментариев поста также работает, но я не могу распечатать ошибки.
Модель комментария:
class Comment
belongs_to :post
validates :commenter, :presence => true
end
Контроллер комментариев:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
if @comment.save
redirect_to post_path(@post)
else
render :template => 'posts/show'
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
render :template => 'posts/show'
end
Почтовый контроллер:
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
def show
@post = Post.find(params[:id])
@comment = @post.comments.build #added
#@comment = @Comment.new #added
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
def edit
@post = Post.find(params[:id])
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
А я использую в виде:
<%= form_for([@post, @post.comments.build]) do |f| %>
Как я могу получить ошибки для комментариев? Если я пытаюсь, я всегда получаю: undefined метод `errors 'или nil object.
Пожалуйста, помогите, я совершенно новичок в рельсах.
Спасибо!
Picki