0

这是我的模态文件 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。有人可以指导我吗?这将有很大帮助。

4

1 回答 1

0

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

您需要删除=标志

回答你的第二个问题

<%= form_tag :action => 'uploadFile' do %>有故障

  • 打开你的控制台
  • cd在您的应用程序目录中:cd \Users\pratik\RubymineProjects\upload
  • 类型rake routes
  • 您会在很多行中看到类似于(您的输出可能不同)的内容:

    upload GET    /upload(.:format)    upload#index
           POST   /upload(.:format)    upload#create
    
  • 在“GET”的左侧,注意出现的名称(在我的示例中,它的“上传”可能与您的输出不同)

  • 然后最后form_tag通过附加在您的视图中使用此名称_path,因此您应该有这样的结果:<%= form_tag upload_path do %>
于 2013-07-24T03:01:19.823 回答