0

我正在 ruby​​ mine 中构建一个 ROR 应用程序。但以下似乎没有运行`

<%= form_tag upload_index_path({:action => 'uploadFile'},
                               :multipart => true) do  %>
<p><label for="upload_file">Select File</label> :
  <%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>`

This code is in my view file. I am trying to build a site where i can upload files. Everything appears good but when i click the upload button the file doesn't get uploaded only the url changes. Why is that??

这是我的控制器代码

class UploadController < ApplicationController
  def index
    render :file => 'app\views\upload\uploadfile.html.erb'
  end
  def uploadFile
    post = DataFile.save(params[:upload])
    render :text => "File has been uploaded successfully"
  end
end

这是我的模型代码

class DataFile < ActiveRecord::Base
  # attr_accessible :title, :body
  def self.save(upload)
    name =  upload['datafile'].original_filename
    directory = "public/data"
    # create the file path
    path = File.join(directory, name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
  end
end

我一直无法找到解决方案。我已经尝试了一个星期。

4

1 回答 1

0

在您的 form_tag 声明中,从路径选项中删除多部分,如下所示:

<%= form_tag upload_index_path({:action => 'uploadFile'}) , :multipart => true do  %>                    
    <p>
        <label for="upload_file">Select File</label> :
        <%= file_field 'upload', 'datafile' %>
    </p>

    <%= submit_tag "Upload" %>

<% end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

于 2013-07-24T07:49:19.393 回答