I have a Student model and an AddlDocument model (based on paperclip).
class Student < ActiveRecord::Base
has_many,:addl_documents, :as => 'receiver', :dependent =>
:destroy
accepts_nested_attributes_for, :addl_documents, :allow_destroy =>
true
end
class AddlDocument < ActiveRecord::Base
has_attached_file :doc_attachment,
:url =>
“/system/:class/:attachment/:id/:style/:basename.:extension”,
:path =>
“:rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension”
belongs_to :receiver, :polymorphic=>true
end
I want to add multiple attachments for a particular student. Here is the
form :
<% form_for (@asset,:url=>{:controller=>'addl_documents',
:action=>‘save_a’}, :html=>{:multipart=>true}) do |f| %>
<%= f.hidden_field :id, :value=>@document.id %>
<%= label_tag :document_type %>
<%= select_tag ‘document_type’,
options_for_select(@doc_types.collect{ |doc| [doc.document_type, doc.id]
}) %>
<%= label_tag :description %>
<%= text_area_tag ‘description’, nil,:cols => 25, :rows => 4 %>
<%= label_tag :notes %>
<%= text_area_tag ‘notes’, nil,:cols => 25, :rows => 4 %>
<% f.fields_for :addl_documents do |d| %>
<%= render “addl_document_fields”, :f=>d %>
<% end %>
<div class="add_addl_attachment">
<%= link_to_add_addl_attachment "#{image_tag "buttons/add_2.png"
} Add", f, :addl_documents %>
<%=submit_tag “#{t(‘send’)}”, :class => ‘send_button’,
:disable_with => “#{t(‘please_wait’)}” %>
<% end %>
Then I go to this forms page, I fill in the fields, and click on the
submit button. The server try to renders the page, but it doesn’t work
because I have the following error :
undefined method `klass' for nil:NilClass
My partial page(_addl_document_fields) contains the following
<div class="fields">
<div class="label-field-pair-attachment">
<label for="student_image_file"><%= t('attachment') %></label>
<% if @asset.new_record? %>
<div class="text-input-bg">
<%= f.file_field :doc_attachment, :size=>12 %></div>
<% else %>
<% if f.object.attachment_file_name.nil? %>
<div class="text-input-bg"><%= f.file_field
:doc_attachment, :size=>12 %>
<% else %>
:length => 15,:ommision => ‘…’) %>
image_tag(‘buttons/delete_2.png’) ,f %>
<% end %>
<% end %>
My controller code are as follows:
def new
@doc_types = DocumentSetting.find(:all, :order => 'document_type
asc’,:conditions => “status = true”)
@document = Student.find(params[:id])
@asset = @document.addl_documents.build
end
def save_a
if request.post?
@document = Student.find(params[:addl_document][:id])
@asset =
AddlDocument.new(params[:addl_document][:addl_documents])
@asset.save
end
end
I am not sure whether I have set up the associations correctly and
written the controller code accordingly as I am just learning Rails
associations and new to ROR. Please help.