我知道这里有几个不同的帖子,人们试图让他们的带有嵌套模型的 Rails 3 表单“工作”。我看过很多,包括Subclass Is Not Saving和Nested Model with Polymorphic Association,以及 Ryan Bates 的 Railscasts on Nested Model Forms和他关于 Complex Forms 的两个教程。
也就是说,我很难理解为什么我的表单中的对象没有保存。
这是我的模型:
课程.rb
class Course < ActiveRecord::Base
attr_accessible :course_name, :course_semester, :course_summary, :course_year, :objectives_attributes
has_many :objectives, as: :objectiveable
accepts_nested_attributes_for :objectives, :reject_if => lambda { |a| a[:objective].blank? }, :allow_destroy => true
目标.rb
class Objective < ActiveRecord::Base
attr_accessible :objective
belongs_to :objectiveable, polymorphic: true
我的表格:
新的.html.erb
<%= form_for [current_user, @course] do |course| %>
<%= course.label "Course Name" %>
<%= course.text_field :course_name, id:"course_name" %>
<%= course.label "Semester" %>
<%= course.select(:course_semester,[['Fall', 'Fall'], ['Spring', 'Spring'], ['Summer', 'Summer'], ['Winter','Winter']] ) %>
<%= course.label "Course Year" %>
<%= course.text_field :course_year, id:"course_year" %>
<p>Course Objectives</p>
<%= course.fields_for :objectives do |objective| %>
<%= objective.text_field :objective, name: "objective" %>
<% end %>
<%= course.label "Course Summary" %>
<%= course.text_area :course_summary, id:"course_summary" %>
<%= course.submit "Save and Return", name: "save_and_return" %>
<%= course.submit "Create a Unit", name: "course_to_unit" %>
<% end %>
我的控制器:
course_controller.rb
class CoursesController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def new
@course = current_user.courses.new
@course.objectives.build
end
def create
@course = current_user.courses.new(params[:course])
if @course.save && params[:save_and_return]
redirect_to user_path(current_user)
elsif @course.save && params[:course_to_unit]
redirect_to new_course_unit_path(@course)
else
flash[:notice] = "Sorry, there was a mistake with the form"
render 'new'
end
end
我的日志:
测试日志
Started POST "/users/1/courses" for 127.0.0.1 at 2012-11-27 22:23:00 -0500
Processing by CoursesController#create as HTML
Parameters: {"utf8"=>"✓", "course"=>{"course_name"=>"Physics 1", "course_semester"=>"Fall", "course_year"=>"2012", "course_summary"=>"This is a valid course summary."}, "objective"=>"An objective", "save_and_return"=>"Save and Return", "user_id"=>"1"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
[1m[35m (0.0ms)[0m begin transaction
[1m[36mSQL (0.7ms)[0m [1mINSERT INTO "courses" ("course_name", "course_semester", "course_summary", "course_year", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?)[0m [["course_name", "Physics 1"], ["course_semester", "Fall"], ["course_summary", "This is a valid course summary."], ["course_year", 2012], ["created_at", Wed, 28 Nov 2012 03:23:00 UTC +00:00], ["updated_at", Wed, 28 Nov 2012 03:23:00 UTC +00:00], ["user_id", 1]]
[1m[35m (6.1ms)[0m commit transaction
Redirected to http://www.example.com/users/1
Completed 302 Found in 17ms (ActiveRecord: 7.0ms)
我可以在日志中的参数哈希中看到目标,但是在提交数据时,没有任何内容被插入到目标表中。在我的控制台中,我可以使用@course.objectives.create 或@course.objectives.build 创建目标,因此我知道该关联正在工作(另外我已经在Rspec 中测试了模型)。但我无法弄清楚我在表单级别做错了什么。
我还应该注意,我在 Rspec 中将其作为测试运行,如下所示:
course_pages_spec.rb
it "Adds at least one objective to the course" do
expect {
fill_out_course_form_with_valid_info
click_button save_button
}.to change(Course, :count).by_at_least(1)
Course.last.objectives.count.should == 1
end
虽然我也启动了 Rails Server,但在表单中输入值并获得相同的结果。
在此先感谢您的帮助。