I got this error referring to line 3 in the view when I used a form in new action to upload photos.I tried to get multiple photo uploaded. Photo is attached to the item and the item is added to the poll. The relavant codes are:
-# new view
%h1 Upload item pictures
= form_for(@poll, :html => {:multipart => true }) do |f|
- @poll.errors.full_messages.each do |msg|
%p = msg
%fieldset
= f.label :title
= f.text_field :title
%fieldset
Pending Attachments: (Max of #{Item::Max_Attachments}) each under #{Item::Max_Attachment_Size/1.megabyte} MB)
- if @poll.items.count >= Item::Max_Attachments
<input id="newfile_data" type="file" disabled />
- else
%input{:id => "newfile_data", :type => "file"}
#attachment_list
%ul{:id => "pending_files"}
%fieldset
= f.submit "Create"
class PollsController < ApplicationController
# POST /polls
def create
@poll = Poll.new(params[:poll])
process_file_uploads(@poll)
if @poll.save
flash[:notice] = 'poll was successfully created.'
redirect_to(@poll)
else
render :action => "new"
end
end
private
def process_file_uploads(poll)
i = 0
while params[:attachment]['file_'+i.to_s] != "" && !params[:attachment]['file_'+i.to_s].nil?
poll.items.build(:photo => params[:attachment]['file_'+i.to_s])
i += 1
end
end
end
class Poll < ActiveRecord::Base
has_many :items, :dependent => :destroy
accepts_nested_attributes_for :items
end
Are there anything wrong in my codes? Thanks!
I got it. Just need to define new in polls_controller like
And it works!