[Rails] Re: help needed to create a complex form
Hi, Be sure you have build the document for message before you use it. On 8月19日, 下午1时08分, Tom Mac wrote: > 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 > -- > Posted viahttp://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: help needed to create a complex form
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 -- 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 post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: help needed to create a complex form
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 On Aug 18, 1:43 pm, Tom Mac wrote: > 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 > -- > Posted viahttp://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.