2012-01-04 71 views
1

Rails 3.1,紅寶石1.87(不要恨我)。我觀看了嵌套表單上的Railscasts;我只把它放在裏面,這樣你就可以指出我可能在Railscast中錯過了什麼,如果你知道它們的話。Rails 3嵌套形式;未知的屬性錯誤新

注意:我添加了@sample_data_set.build_sample_data_template,但是在帖子上得到了「未知屬性:sample_data_set_id」[代碼也隨後發佈了新內容)。

在Create/New上使用嵌套表單;點擊提交,並得到:

的ActiveRecord :: UnknownAttributeError(未知屬性: sample_data_templates):
應用程序/控制器/ sample_data_sets_controller.rb:50:在new'
app/controllers/sample_data_sets_controller.rb:50:in
打造」

樣本數據集模型:

class SampleDataSet < ActiveRecord::Base 
    has_one :sample_data_template, :dependent => :destroy 
    accepts_nested_attributes_for :sample_data_template 
end 

樣本數據模板型號:

class SampleDataTemplate < ActiveRecord::Base 
    belongs_to :sample_data_set 
    #Random info generation 
    def self.name_gen(*prepend) 
    character_map = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten 
    name = (0..8).map{ character_map[rand(character_map.length)] }.join 

    if prepend[0].nil? || prepend[0] == "" 
     return name 
    else 
     return prepend[0].to_s + "_" + name 
    end 
    end 

    def self.ssn_gen 
    #broke this out as its own method in case someone wants some logic later one 
    ssn = "" 
    3.times do 
     ssn = ssn + (100..999).to_a.choice.to_s 
    end 
    return ssn 
    end 

    def self.row_gen(row_count) 
    @data_rows = Array.new 
    i = 0 
    until i > row_count do 
     @row = SampleDataSet.first 
     @row.officialFirstName = SampleDataTemplate.name_gen 
     @row.officialLastName = SampleDataTemplate.name_gen 
     @row.emailAddresses  = @row.officialFirstName + @row.officialLastName + "@aaa.aaa.edu" 
     @row.ssn    = SampleDataTemplate.ssn_gen 
     @data_rows << @row 
     i += 1 
    end 

    return @data_rows 
    end 
end 

樣本數據控制器#新

def new 
    @sample_data_set = SampleDataSet.new 
    @sample_data_set.build_sample_data_template #after adding this I get error:unknown attribute: sample_data_set_id  
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render :json => @sample_data_set } 
    end 

樣本數據控制器#創建

def create 
    @sample_data_set = SampleDataSet.new(params[:sample_data_set]) 

    respond_to do |format| 
     if @sample_data_set.save 
     format.html { redirect_to @sample_data_set, :notice => 'Sample data set was successfully created.' } 
     format.json { render :json => @sample_data_set, :status => :created, :location => @sample_data_set } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @sample_data_set.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
    end 

更新,添加形片

<div class="sample_fields"> 
    <%= f.fields_for :sample_data_templates do |builder| %> 
     <%= render "sample_data", :f => builder%> 
    <% end %> 
    </div> 

更新,模式:

ActiveRecord::Schema.define(:version => 20120103172936) do 

    create_table "sample_data_sets", :force => true do |t| 
    t.string "title" 
    t.text  "description" 
    t.string "created_for" 
    t.string "created_by" 
    t.integer "number_of_records" 
    t.integer "sample_data_template_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "sample_data_templates", :force => true do |t| 
    t.integer "sample_data_set_id" 
    t.string "irn" 
    t.string "ssn" 
    t.string "officialLastName" 
    t.string "officialFirstName" 
    t.string "emailAddresses" 
    t.string "campusNum" 
    t.string "internationalId" 
    t.string "internationalIdCountry" 
    t.string "gender" 
    t.string "officialMiddleInitial" 
    t.string "previousLastName" 
    t.string "previousFirstName" 
    t.string "previousMiddleInitial" 
    t.string "addressLine1" 
    t.string "addressLine2" 
    t.string "addressLine3" 
    t.string "city" 
    t.string "state" 
    t.string "zipCode" 
    t.string "province" 
    t.string "homeAreaCode" 
    t.string "homePhoneNumber" 
    t.string "homePhoneExtenstion" 
    t.string "homePhoneCountryCode" 
    t.string "workAreaCode" 
    t.string "workPhoneNumber" 
    t.string "workExtenstion" 
    t.string "workPhoneCountryCode" 
    t.string "faxAreaCode" 
    t.string "faxPhoneNumber" 
    t.string "faxExtension" 
    t.string "faxCountryCode" 
    t.string "race" 
    t.string "previousDegree" 
    t.string "region" 
    t.string "foreignTranscript" 
    t.string "apolloEmployee" 
    t.string "nursingLicenseExpiration" 
    t.string "nursingInsuranceExpiration" 
    t.string "otherInsuranceExpiration" 
    t.string "program" 
    t.string "version" 
    t.string "groupId" 
    t.string "team" 
    t.string "enrollmentUserId" 
    t.string "admissionsUserId" 
    t.string "oldProgram" 
    t.string "oldVersion" 
    t.string "galaxyStudentOid" 
    t.string "suffixOne" 
    t.string "suffixTwo" 
    t.string "employeId" 
    t.string "promoCode" 
    t.string "revCampusOid" 
    t.string "FerpaNotes" 
    t.string "isWavierHigh" 
    t.string "executingUserId" 
    t.string "totalDeclaredExtCredits" 
    t.datetime "insuranceExpireDate" 
    t.datetime "acknowledgementDate" 
    t.datetime "scheduledReentryDate" 
    t.datetime "scheduledStartDate" 
    t.datetime "dateOfBirth" 
    t.datetime "enrollAgreeSignDate" 
    t.boolean "usCitizen" 
    t.boolean "financialAid" 
    t.boolean "overrideFlag" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

回答

2

sample_data_templates表有一個sample_data_set_id列嗎?也許你沒有將它添加到遷移中,或者沒有運行遷移?

+0

那麼我現在...拼寫錯誤的樣本,並已糾正現在。仍然得到錯誤。將用兩個表的列名更新。 更新:添加模式 – ScottJShea 2012-01-04 18:52:06

+0

在我的表單中我有':sample_data_templates'而不是':sample_data_template'。 SMH。感謝@miked提出的問題讓我更接近我的代碼 – ScottJShea 2012-01-04 19:07:34