Rails 3.1, Ruby 1.87 (don’t hate me). I watched the Railscasts on Nested Forms; I only put that in so you can point out where I might have missed something in the Railscasts if you know them.
Note: I added @sample_data_set.build_sample_data_template but got “unknown attribute: sample_data_set_id” on the post instead [the code is also posted with the new below).
Using a Nested form on Create/New; hit Submit and get:
ActiveRecord::UnknownAttributeError (unknown attribute:
sample_data_templates):
app/controllers/sample_data_sets_controller.rb:50:innew'create’
app/controllers/sample_data_sets_controller.rb:50:in
Sample Data Set Model:
class SampleDataSet < ActiveRecord::Base
has_one :sample_data_template, :dependent => :destroy
accepts_nested_attributes_for :sample_data_template
end
Sample Data Template Model:
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
Sample Data Controller#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
Sample Data Controller#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
Update, added form piece
<div class="sample_fields">
<%= f.fields_for :sample_data_templates do |builder| %>
<%= render "sample_data", :f => builder%>
<% end %>
</div>
Update, Schema:
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
Does sample_data_templates table have a sample_data_set_id column? Perhaps you didn’t add it to the migration or did not run the migration?