views:

72

answers:

1

I am following alloy-complex-form-examples from github trying to learn nested forms and I just cannot get this thing to work. http://github.com/alloy/complex-form-examples

NoMethodError in Projects#new

Showing app/views/projects/_form.html.erb where line #13 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.new_record?

Extracted source (around line #13):

10:   </p>
11:   
12:   <p>
13:     <% project_form.fields_for :author do |author_form| %>
14:       <%= author_form.label :name, "Author name:" %>
15:       <%= author_form.text_field :name %>
16:       

Trace of template inclusion: app/views/projects/new.html.erb

class Comment < ActiveRecord::Base
  belongs_to :post
end


class Post < ActiveRecord::Base
  validates_presence_of :name, :title  
  validates_length_of :title, :minimum => 5 
  has_many :comments
  has_many :tags

  accepts_nested_attributes_for :tags, :allow_destroy => :true ,  
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Tag < ActiveRecord::Base
  belongs_to :post
end

Anyone have any advice? I have been trying to get nested forms to work all day and I have been unsuccessful.

projectsController

class ProjectsController < ApplicationController
  def index
    @projects = Project.find(:all)
  end

  def show
    @project = Project.find(params[:id])
  end

  def new
    @project = Project.new
    @project.tasks.build
    @project.tags.build
  end

  def create
    @project = Project.new(params[:project])
    if @project.save
      flash[:notice] = "Successfully created project."
      redirect_to @project
    else
      render :action => 'new'
    end
  end

  def edit
    @project = Project.find(params[:id])
    # add an extra new record for debugging purposes
    @project.tasks.build
    @project.tags.build
  end

  def update
    @project = Project.find(params[:id])
    @project.attributes = params[:project]
    if @project.save
      flash[:notice] = "Successfully updated project."
      redirect_to @project
    else
      render :action => 'edit'
    end
  end

  def destroy
    @project = Project.find(params[:id])
    @project.destroy
    flash[:notice] = "Successfully destroyed project."
    redirect_to projects_url
  end
end
+3  A: 

Your author_form needs an author to work with. Either you build it right before line 13:

<% @project.build_author unless @project.author %>
<% project_form.fields_for :author do |author_form| %>
  <%= author_form.label :name, "Author name:" %>
  <%= author_form.text_field :name %>

Or you build it in your ProjectsController:

def new
  @project = Project.new
  @project.tasks.build
  @project.tags.build

  @project.build_author unless @project.author
end

BTW, that "@project.build_author unless @project.author" was in the original files for complex-form-examples. You may have deleted it while editing them.

agregoire
Works, thank you!
Jason
question: I tried adding @project.author.build but that didn't work. Why do you use .build_author on this one? Is it because project belongs_to author?
Jason
@project.author.build calls the build method on the project's author. Since your project has no author, it tries to call "build" on nil. Which doesn't work.The build_author method is added to your Project model by ActiveRecord through the belongs_to relationship. (read about that here: http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference)
agregoire