Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
attachment_fu+ complex forms
2 Posts
attachment_fu+ complex forms

Hi i have an issue of saving empty file in complex-froms.

I used attachment_fu to upload files. My concern is when ever i submit idea from without uploading a file, all values of idea are getting saved except mugshot. I have not used any validations for mugshot.

My requirement is when ever submit the form without uploading a file, in mugshots table a record need to be create with idea_id and all file column will be fill with null.

hope u undersatnd my requirement...........


class Idea < ActiveRecord::Base has_many :mugshots accepts_nested_attributes_for :mugshots (for complex-forms) end

class Mugshot < ActiveRecord::Base belongs_to :idea has_attachment :content_type => [:image,'application/pdf', 'application/msword','application/vnd.ms-excel', 'plain/text', 'application/msexcel' ,'text/plain', 'xls', 'xlsx'],

             :storage => :file_system, 
             :max_size => 2.megabyte,
             :resize_to => '320x200>',
             :thumbnails => { :thumb => '100x100>' }


---------------end of models-----------------

-------------- controllers--------------- class IdeasController < ApplicationController

def new

 @idea = Idea.new


def create

 def create
  @idea = Idea.new(params[:idea])


end ----------------end of controller--------------

-----------------views------------------------ app/views/ideas/new.html.haml

  • form_for @idea, :html => {:multipart => "true"} do |f| -f.fields_for :mugshots do |mugshot_form| %p =mugshot_form.label :uploaded_data, "Add Extra file" %br =mugshot_form.file_field :uploaded_data f.submit "save"


In your Idea class, you should use anaf like this: accepts_nested_attributes_for : mugshots, :reject_if => lambda{ |m| m["uploaded_data"].blank? }

It should reject mugshots without file.

2 Posts
Login to add your message