# Help needed to create a complex form

**URL:** <https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464>\
**Category:** Rails\
**Created:** [August 18, 2010, 7:43am UTC](https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464 "2010-08-18T07:43:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [August 18, 2010, 7:43am UTC](https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464/1 "2010-08-18T07:43:23Z")

</div>

Hi  
My models are message\_thread, message, document (document with  
attachments I am using paperclip)

# message\_thread

has\_many messages

# message

belongs\_to message\_thread  
has\_many :document\_messages  
has\_many :documents, :through =\> :document\_messages

# document

has\_attached\_file :attachment,  
:storage =\> :database  
has\_many :document\_messages  
has\_many :messages, :through =\> :document\_messages

```
 What i need is to create a message_thread with one message and that

```

message with 2(or more) attachments. I tried like

# messages\_controller

def new  
@message\_thread = MessageThread.new  
@message = @message\_thread.messages.build  
end

# new.html.erb

\<%form\_for @message\_thread, :url =\> { :action =\> “create” },:html =\>  
{:multipart =\> true,:class =\> “mail-tenant”} do |f| %\>

\<%= f.text\_field :mail\_to %\>

  

\<% f.fields\_for :messages do |message| %\>  
\<%= message.label :subject,‘Subject’ %\>  
\<%= message.text\_field :subject,:maxlength =\> 255 %\>

  

  

**Attachments:**

  
\<% message.fields\_for :documents do |document| %\>  

  
\<%= document.file\_field :attachment,:class =\> “file” %\>  
\<%= document.hidden\_field :company\_id,:value =\>  
current\_company.try(:id)%\>  

  
\<%end%\>   
\<%end%\>   

  

  
Send  

  

Or Cancel

  
\<%end%\> 

```
       But I am getting error

```

ActiveRecord::AssociationTypeMismatch in MessagesController#create

Document(#-614356858) expected, got Array(#-608120208)

Please help me to solve this. I have a vague idea of using  
accepts\_nested\_attributes\_for . But please tell here can I use it

Thanks in advance

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [August 18, 2010, 3:13pm UTC](https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464/2 "2010-08-18T15:13:22Z")

</div>

Hi,

There is an example for nested file upload form by using paperclip.  
The codes are similar to you, be attention to the usage of  
accepts\_nested\_attributes\_for.

Check it here:  
[http://rails-bestpractices.com/posts/45-use-sti-and-polymorphic-model-for-multiple-uploads](http://rails-bestpractices.com/posts/45-use-sti-and-polymorphic-model-for-multiple-uploads)

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [August 19, 2010, 7:08am UTC](https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464/3 "2010-08-19T07:08:21Z")

</div>

Hi  
Thanks for replying.I could do it partially with the following  
modifications to my models and views

# message\_thread

has\_many messages  
accepts\_nested\_attributes\_for :messages

# message

belongs\_to message\_thread  
has\_many :document\_messages  
has\_many :documents, :through =\> :document\_messages  
accepts\_nested\_attributes\_for :documents,:reject\_if =\> lambda {|a|  
a[:attachment].blank?}  
validates\_presence\_of :subject

```
  No other change. But the problem is from the view I am not 

```

selecting any file for upload and not entering values for ‘subject’ .  
So surely the validation will fail and rerender my new action . My  
‘create’ action like

def create  
@message\_thread = MessageThreads.new(params[:message\_thread])  
if @message\_thread.save  
flash[:notice] = “Message sent successfully”  
redirect\_to home\_url  
else  
flash.now[:error] = @message\_thread.errors.full\_messages.join("  
")  
render :action =\> ‘new’  
end  
end

```
  The problem is now the file_field disappears. What might be the 

```

cause?I am struggling with this for hours.Please help

Thanks

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [August 19, 2010, 8:12am UTC](https://www.ruby-forum.com/t/help-needed-to-create-a-complex-form/193464/4 "2010-08-19T08:12:38Z")

</div>

Hi,

Be sure you have build the document for message before you use it.
