gpt4 book ai didi

ruby-on-rails - accepts_nested_attributes_for 和 has_many 多态

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:51 24 4
gpt4 key购买 nike

我使用回形针上传附加到学生类(class)作业的多个文件,但我失败了。

型号

class StudentCourseAssignment < ActiveRecord::Base
attr_accessible :score, :comment, :finish_status,:attachments
accepts_nested_attributes_for :attachments
belongs_to :assignment
belongs_to :user
has_many :attachments ,:as => :attachmentable,:dependent => :destroy
end

class Attachment < ActiveRecord::Base
attr_accessible :user_upload
belongs_to :attachmentable , :polymorphic => true
has_attached_file :user_upload
end

控制者

**new**

@sca = StudentCourseAssignment.new
@sca.attachments.build
@sca.attachments.build

**create**

@sca = StudentCourseAssignment.new(params[:student_course_assignment])
@assignment = Assignment.find(params[:assignment_id])
@sca.user = current_user
@sca.assignment = @assignment
if @sca.save
flash[:alert] = "success"
redirect_to course_years_path
else
flash[:alert] = "fail"
redirect_to course_years_path
end

** 查看**

<%= form_for @sca, :url => assignment_student_course_assignments_path(@assignment),
:html => { :id => 'student-assignment-form', :multipart => true } do |f| %>
file:
<%= f.fields_for :attachments do |a_f| %>
<%= a_f.file_field :user_upload %>
<%= submit_tag "create" %>
<% end%>
<% end %>

错误

No association found for name `attachments'. Has it been defined yet?

如果去掉accepts_nested_attributes_for :attachments,还是错误

Attachment(#70201401779680) expected, got Array(#70201383294620)

希望得到您的帮助!谢谢!

最佳答案

改变

来自:

attr_accessible :score, :comment, :finish_status,:attachments

到:

attr_accessible :score, :comment, :finish_status,:attachments_attributes

关于ruby-on-rails - accepts_nested_attributes_for 和 has_many 多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14017374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com