0

我是 Ruby 和 Ruby on Rails 的新手。

我正在实施前一段时间百思买使用的旧 BBYIDX 网站的一个版本。该应用程序基于 Rails 2.3.11,因此许多有关文件上传的较新教程对我没有帮助。

如果用户选择,我希望包括一个上传文件的选项,以及他们提出的建议。

当前在控制器中使用的代码,ideas_controller.rb 是:

def uploadFile
post = idea.save(params[:upload])
render :text => "File has been uploaded successfully"
end

模型idea.rb中使用的代码是:

attr_accessor :upload
def self.save(upload)
name = upload['idea'].original_filename
directory = 'public/data'
# create the file path
path = File.join(directory,name)
# write the file
File.open(path, "wp") { |f| f.write(upload['datafile'].read)}
end

视图文件 _new.html.haml 中使用的代码是:

-# This partial shared by /ideas/new, home page, and drop-down.
= error_messages_for :idea
- remote_form_for :idea, :url => ideas_path, :action => 'uploadFile', :update => 'submit-   idea', :has_example_text => true do |f|
- if defined?(current) && !current.nil?
=f.hidden_field :current_id, :id => "idea-current-id", :value=>current.id
- else
=f.hidden_field :current_id, :id => "idea-current-id"
#idea-title-group.form-group.inline
= f.label :title, "Idea"
= f.text_field :title, :example => 'My idea is...', :maxlength => 120, :size => 51, :id => "idea-    title", :class => "input-text big"
#idea-description-group.form-group.inline
= f.label :description, "Description"
= f.text_area :description, :example => "I would like to suggest...", :id => "idea-  description", :class => "input-textarea big", :rows => "7", :cols => "40"

#idea-title-group.form-group.inline
  = f.file_field :upload
  -#= f.submit "Upload file"

#idea-tags-group.form-group.inline
%label{:for => "tags"} Tags
= render :partial => 'ideas/tag_editor', :locals => { :f => f }
= submit_tag 'Share My Idea', :class => "input-submit big"

%p.note>
%strong.alert<>
  Remember
, please keep it constructive!

上传命令以 f.file_field :upload 区域下的形式发出。我的代码有问题吗?运行应用程序并尝试上传文件不会导致任何问题或崩溃,但不会上传文件。该视图按我的意愿显示,并且出现了我可以在我的机器上浏览文件的框,但上传没有通过。

我知道 multipart => true 部分也需要添加到某个地方,但我不知道如何在现有的远程表单中实现它。

任何帮助都会受到欢迎,因为我真的不知道我在做什么。

4

0 回答 0