2

I use paperclip to upload multi-file attached to studentcourseassignment,but i fail.

model

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

controller

**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

** view**

<%= 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 %>

wrong

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

if remove accepts_nested_attributes_for :attachments,it's still wrong

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

hope your help!thx!

2 Answers 2

2

Change

from:

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

to:

attr_accessible :score, :comment, :finish_status,:attachments_attributes
Sign up to request clarification or add additional context in comments.

Comments

1

I realize this is an old question, but fwiw I think you'll need to move

accepts_nested_attributes_for :attachments

to appear after

has_many :attachments, :as => :attachmentable,:dependent => :destroy

I hit this in a project once myself; pretty sure it boils down to accepts_nested_attributes_for expecting the relation to already be declared before its invoked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.