Rails 3嵌套表格; New上的UnknownAttributeError

时间:2012-01-04 18:10:58

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

Rails 3.1,Ruby 1.87(不要讨厌我)。我在Nested Forms上观看了Railscasts;我只是把它放进去,所以如果你知道的话,你可以指出我在Railscasts中可能遗漏的东西。

注意:我添加了@sample_data_set.build_sample_data_template,但在帖子上有“未知属性:sample_data_set_id”[代码也会在下面发布新代码。)

在Create / New上使用嵌套表单;点击提交并获取:

  

ActiveRecord :: UnknownAttributeError(未知属性:   sample_data_templates):
  app / controllers / sample_data_sets_controller.rb:50:in new'
app/controllers/sample_data_sets_controller.rb:50:in
create'

示例数据集模型:

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

示例数据控制器#New

  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

示例数据控制器#Create

  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

1 个答案:

答案 0 :(得分:2)

sample_data_templates表是否有sample_data_set_id列?也许你没有将它添加到迁移中或者没有运行迁移?