2009-11-22 11 views
1

github에서 중첩 된 양식을 배우려고 시도하는 합금 - 복합 - 양식 예제를 따르고 있으며이 작업을 수행 할 수 없습니다. http://github.com/alloy/complex-form-examplesNoMethodError in projectss # new

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 

누구나 어떤 조언을? 나는 하루 종일 작동하도록 중첩 된 양식을 얻으려고 노력했지만 실패했습니다.

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 
+0

ProjectsController에있는 것을 보여줄 수 있습니까? – agregoire

+0

ProjectsController – Jason

답변

3

귀하의 author_form는 작업을하는 작가를 필요로한다.

<% @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 %> 

을 또는 당신은 당신의 ProjectsController에 구축 : 어느 당신은 바로 전에 라인 (13)을 구축 "project.author @ @의 project.build_author하지 않는 한"원본 파일에 있었던,

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

    @project.build_author unless @project.author 
end 

BTW 복합 형 예제의 경우 편집 중에 삭제했을 수 있습니다.

+0

작품, 고맙습니다! – Jason

+0

질문 : @ project.author.build를 추가하려고 시도했지만 작동하지 않았습니다. 왜이 .build_author를 사용합니까? 그것은 프로젝트가 belongs_to 저자이기 때문에 그것입니까? – Jason

+1

@ project.author.build는 프로젝트 작성자의 빌드 메소드를 호출합니다. 프로젝트에는 작성자가 없으므로 "빌드"를 호출하지 않습니다. 어느 쪽이 작동하지 않습니다. build_author 메소드는 belongs_to 관계를 통해 ActiveRecord에 의해 프로젝트 모델에 추가됩니다. (여기에 대해 읽어보십시오 : http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference) – agregoire