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 に答える