ruby on rails has_many relation form validation of children

picki Source

I followed the tutorial to create the blog application. So I have posts and comments. The validation for fields in the posts form works perfect. The validation in the comments section of a post works as well, but I cant get the errors to be printed.

The comment model:

    class Comment 
      belongs_to :post  
      validates :commenter, :presence => true  
    end

The comment controller:

  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

The post controller:

  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

And I use in the form:

<%= form_for([@post, @post.comments.build]) do |f| %>

How would I get the .errors for the comments? If I try I always get: undefined method `errors' or nil object.

Please help, I am completely new to rails.

Thanks!

Picki

ruby-on-rails

Answers

answered 7 years ago polarblau #1

The problem is, that you're building the comment new every time you're loading the form. Like this, the comment with the validation errors never makes it into your form.

Create the comment instead in your controller, something like this — details depending on your application:

# posts controller
def show
  @post = Post.find(params[:id])
  @comment = Comment.new
end

# comments controller
def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.build(params[:comment])
  if @comment.save
    redirect_to @post
  else
    render :new
  end
end

And change your form:

<%= form_for([@post, @comment]) do |f| %>

comments powered by Disqus