0

当我在新操作中使用表单上传照片时,我在视图中的第 3 行出现此错误。我试图上传多张照片。照片附在项目上,项目被添加到投票中。相关代码为:

-# 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

我的代码有什么问题吗?谢谢!

4

1 回答 1

0

我得到了它。只需要在 polls_controller 中定义 new 就像

def new
  @poll = Poll.new 
end

它有效!

于 2012-11-11T19:34:44.517 回答