Ruby on rails NoMethodError Rails多文件上传
我正在为我的应用程序中的一个模型进行多个文件上传,我包含了以下代码: 交付控制器.rbRuby on rails NoMethodError Rails多文件上传,ruby-on-rails,attachment,Ruby On Rails,Attachment,我正在为我的应用程序中的一个模型进行多个文件上传,我包含了以下代码: 交付控制器.rb # POST /delivers def create @deliver = Deliver.new(params[:deliver]) process_file_uploads(@deliver) if @deliver.save flash[:notice] = 'Task was successfully created.' redirect_to(@deliver)
# POST /delivers
def create
@deliver = Deliver.new(params[:deliver])
process_file_uploads(@deliver)
if @deliver.save
flash[:notice] = 'Task was successfully created.'
redirect_to(@deliver)
else
render :action => "new"
end
end
protected
def process_file_uploads(deliver)
i = 0
while params[:attachment]['file_'+i.to_s] != "" && !params[:attachment]['file_'+i.to_s].nil?
deliver.assets.build(:data => params[:attachment]['file_'+i.to_s])
i += 1
end
end
has_many :assets, :as => :attachable, :dependent => :destroy
validate :validate_attachments
Max_Attachments = 5
Max_Attachment_Size = 5.megabyte
def validate_attachments
errors.add_to_base("Too many attachments - maximum is #{Max_Attachments}") if assets.length > Max_Attachments
assets.each {|a| errors.add_to_base("#{a.name} is over #{Max_Attachment_Size/1.megabyte}MB") if a.file_size > Max_Attachment_Size}
end
class AssetsController < ApplicationController
def show
asset = Asset.find(params[:id])
# do security check here
send_file asset.data.path, :type => asset.data_content_type
end
def destroy
asset = Asset.find(params[:id])
@asset_id = asset.id.to_s
@allowed = Deliver::Max_Attachments - asset.attachable.assets.count
asset.destroy
end
end
class Asset < ActiveRecord::Base
has_attached_file :data,
belongs_to :attachable, :polymorphic => true
def url(*args)
data.url(*args)
end
def name
data_file_name
end
def content_type
data_content_type
end
def file_size
data_file_size
end
end
deliver.rb
# POST /delivers
def create
@deliver = Deliver.new(params[:deliver])
process_file_uploads(@deliver)
if @deliver.save
flash[:notice] = 'Task was successfully created.'
redirect_to(@deliver)
else
render :action => "new"
end
end
protected
def process_file_uploads(deliver)
i = 0
while params[:attachment]['file_'+i.to_s] != "" && !params[:attachment]['file_'+i.to_s].nil?
deliver.assets.build(:data => params[:attachment]['file_'+i.to_s])
i += 1
end
end
has_many :assets, :as => :attachable, :dependent => :destroy
validate :validate_attachments
Max_Attachments = 5
Max_Attachment_Size = 5.megabyte
def validate_attachments
errors.add_to_base("Too many attachments - maximum is #{Max_Attachments}") if assets.length > Max_Attachments
assets.each {|a| errors.add_to_base("#{a.name} is over #{Max_Attachment_Size/1.megabyte}MB") if a.file_size > Max_Attachment_Size}
end
class AssetsController < ApplicationController
def show
asset = Asset.find(params[:id])
# do security check here
send_file asset.data.path, :type => asset.data_content_type
end
def destroy
asset = Asset.find(params[:id])
@asset_id = asset.id.to_s
@allowed = Deliver::Max_Attachments - asset.attachable.assets.count
asset.destroy
end
end
class Asset < ActiveRecord::Base
has_attached_file :data,
belongs_to :attachable, :polymorphic => true
def url(*args)
data.url(*args)
end
def name
data_file_name
end
def content_type
data_content_type
end
def file_size
data_file_size
end
end
assets\u controller.rb
# POST /delivers
def create
@deliver = Deliver.new(params[:deliver])
process_file_uploads(@deliver)
if @deliver.save
flash[:notice] = 'Task was successfully created.'
redirect_to(@deliver)
else
render :action => "new"
end
end
protected
def process_file_uploads(deliver)
i = 0
while params[:attachment]['file_'+i.to_s] != "" && !params[:attachment]['file_'+i.to_s].nil?
deliver.assets.build(:data => params[:attachment]['file_'+i.to_s])
i += 1
end
end
has_many :assets, :as => :attachable, :dependent => :destroy
validate :validate_attachments
Max_Attachments = 5
Max_Attachment_Size = 5.megabyte
def validate_attachments
errors.add_to_base("Too many attachments - maximum is #{Max_Attachments}") if assets.length > Max_Attachments
assets.each {|a| errors.add_to_base("#{a.name} is over #{Max_Attachment_Size/1.megabyte}MB") if a.file_size > Max_Attachment_Size}
end
class AssetsController < ApplicationController
def show
asset = Asset.find(params[:id])
# do security check here
send_file asset.data.path, :type => asset.data_content_type
end
def destroy
asset = Asset.find(params[:id])
@asset_id = asset.id.to_s
@allowed = Deliver::Max_Attachments - asset.attachable.assets.count
asset.destroy
end
end
class Asset < ActiveRecord::Base
has_attached_file :data,
belongs_to :attachable, :polymorphic => true
def url(*args)
data.url(*args)
end
def name
data_file_name
end
def content_type
data_content_type
end
def file_size
data_file_size
end
end
new.html.erb(交付视图)
交付存储库
{:multipart=>true})do | f |%>
挂起的附件:(MB以下每个附件的最大值)
=交付::最大附件数%>
Show.html.erb(提供视图)
交付存储库
标题:
正文:
附件:“附件”,:collection=>@deliver.assets%>
|
|
\u attachment.html.erb(交付视图)
引发异常
发生这种情况是因为new.html.erb
中的表单中没有名为attachment
的字段