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
那么我现在...拼写错误的样本,并已纠正现在。仍然得到错误。将用两个表的列名更新。 更新:添加模式 – ScottJShea 2012-01-04 18:52:06
在我的表单中我有':sample_data_templates'而不是':sample_data_template'。 SMH。感谢@miked提出的问题让我更接近我的代码 – ScottJShea 2012-01-04 19:07:34