如何使用ruby mine在目录中上传文件?

时间:2013-07-24 02:50:15

标签: ruby-on-rails file-upload rubymine

这是我的模态文件data_file.rb

    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

这是我的控制器文件

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

这是我的查看文件

 <h1>File Upload</h1>
<%= form_tag :action => 'uploadFile' do  %>
<p><label for="upload_file">Select File</label> :
  <%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<%= end %>

每当我尝试使用语法http://127.0.0.1:3000/upload/index访问视图文件时,我都会收到以下错误

显示C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb第6行引发:

C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:6: syntax error, unexpected keyword_end
');@output_buffer.append= ( end );@output_buffer.to_s
                               ^
C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:7: syntax error, unexpected keyword_ensure, expecting ')'
C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:9: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #6):

3: <p><label for="upload_file">Select File</label> :
4:   <%= file_field 'upload', 'datafile' %></p>
5: <%= submit_tag "Upload" %>
6: <%= end %>

这个项目是tutorialpoints.com上的一个示例项目。但是,当我尝试这样做时,它失败了。我使用ruby mine作为IDE。有人可以指导我吗?这将是非常有帮助的。

1 个答案:

答案 0 :(得分:0)

视图文件的最后一行:<%= end %>

您需要删除=标志

回答您的第二个问题

<%= form_tag :action => 'uploadFile' do %>错误

  • 打开控制台
  • 您的应用程序目录中的
  • cdcd \Users\pratik\RubymineProjects\upload
  • 输入rake routes
  • 你会在许多行中看到一些类似于(你的输出可能不同)的东西:

    upload GET    /upload(.:format)    upload#index
           POST   /upload(.:format)    upload#create
    
  • 在&#34; GET&#34;的左侧
  • ,请注意显示的名称(在我的示例中,其中&#39;上传&#39;您的输出可能会有所不同)

  • 然后最终在您的视图form_tag中使用此名称_path,因此您应该使用此类名称:<%= form_tag upload_path do %>