嵌套表单中的ActiveRecord :: AssociationTypeMismatch错误

时间:2011-12-14 00:43:14

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 nested-forms nested-attributes

当我提交表单时,我收到错误

  

ClassGroupsController #create中的ActiveRecord :: AssociationTypeMismatch   用户(#114808470)预期,得到阵列(#81535960)

这里似乎有什么问题?我正在使用rails 3.1。这是我的代码。提前谢谢

查看

<%= form_for :class_group ,:url=>{:action =>"create"} do |f| %>
  <%= f.text_field :course_id%>
  <%=f.text_field :professor%>
  <%=f.text_area :objective %>
  <%=f.fields_for :users do |c|%>
    <%=c.text_field :sex %>
  <%end%>
  <%=f.submit "post" %>
<%end%>

PARAMS

 "utf8"=>"✓",
 "authenticity_token"=>"UmSPMh9QzgL7FmtBG92kgpzsz7anWc6zcY2KqZJH3Pg=",
 "class_group"=>{
 "course_id"=>"1", 
 "professor"=>"john",
 "objective"=>"Integrity",
 "users"=>{"sex"=>"m"}},
 "commit"=>"post"}

控制器

     class ClassGroupsController < ApplicationController
       def create
         @course=Course.find(params[:class_group][:course_id])
         @class_group=@course.class_groups.build(params[:class_group])
         if @class_group.save
           redirect_to home_url
         end
       end

       def new
         @class_group=ClassGroup.new
         @class_group.users.build
       end
     end

模型

   class ClassGroup < ActiveRecord::Base
     has_many :users ,:through=>:class_memberships
     has_many :class_memberships
     belongs_to :course

     accepts_nested_attributes_for :users
   end

0 个答案:

没有答案