ruby rails
2013-Oct-26 10:02 UTC
polymorphic assocation with nested attributes and paperclip issue
I have a Student model and an AddlDocument model (based on paperclip). <!-- language-all: lang-rb --> 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 %> <div class="addl_attachments"> <% 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 %> </div> </div> <div id="submit-button"> <%=submit_tag "#{t(''send'')}", :class => ''send_button'', :disable_with => "#{t(''please_wait'')}" %> </div> <% 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 %></div> <% else %> <div class="text-input-bg themed_text" style="margin-top: 2px"><%= truncate(f.object.attachment_file_name, :length => 15,:ommision => ''...'') %></div> <div class="remove_link"><%= link_to_remove_fields image_tag(''buttons/delete_2.png'') ,f %></div> <% end %> <% end %> </div> </div> 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2a9281f6aa57fb6d719cd939f74e95a1%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.