I have a Attachment model and Reminder model. The Reminder has many attachments and Attachment belongs to a Reminder. I have set up the association and it works fine. I have defined accepts_nested_attributes_for :attachments in the Reminder model also. I have a nested form where there is multiple file upload fields. I am using paperclip for saving file attachments. I have set up multipart true to the form. But when I submit the form the Reminder model creates an entry in db with Delayed Jobs but in the Attachment model null values are getting saved in the db. Please find the part of my code below. Any inputs /suggestions would be great..
<% form_for @reminder, :url => { :action => "create_reminder" },:html=>{:multipart=>true} do |l| %> <div class="addl_attachments"> <% l.fields_for :attachments do |a| %> <%= render "message_addl_attachment_fields",:f=>a %> <% end %> <div class="add_addl_attachment"> <%= link_to_add_addl_attachment "#{image_tag "buttons/add_2.png" } #{t('add_txt')}", l, :attachments %> </div> </div> </div> <div id="submit-button"> <%=submit_tag "#{t('send')}", :class => 'button', :disable_with => "#{t('please_wait')}" %> </div> <% end %> =====Controller code...
def create_reminder if request.post? @recipients = User.active.find_all_by_id(recipients_array).sort_by{|a| a.full_name.downcase} unless params[:reminder][:body] == "" or params[:recipients] == "" recipients_array = params[:recipients].split(",").reject{|a| a.strip.blank?}.collect{ |s| s.to_i } Delayed::Job.enqueue(DelayedReminderJob.new(:sender_id => @user.id,:recipient_ids => recipients_array,:subject=>params[:reminder][:subject],:body=>params[:reminder][:body])) @reminder.message_addl_attachments.build logger.info "Before saving attachments....#{@reminder.message_addl_attachments.inspect}" @reminder.message_addl_attachments.save logger.info "After Saving attachments....#{@reminder.message_addl_attachments.inspect}"
flash[:notice] = "Message Sent Sucessfully" redirect_to :controller=>"reminder_plugin", :action=>"create_reminder" else flash[:notice]="Please fill the required fields" redirect_to :controller=>"reminder_plugin", :action=>"create_reminder" end end rescue Exception=> e puts "Inside create action.........",e.backtrace end end