2013-07-24 59 views
0

這是我的樣式文件data_file.rb如何使用ruby mine將文件上傳到目錄中?

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作爲IDE。任何人都可以請指導我?這將有很大的幫助。

回答

0

視圖文件的最後一行:<%= end %>

您需要刪除=標誌

回答你的第二個問題

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

  • 打開控制檯
  • cd在您的應用程序目錄:左側

    upload GET /upload(.:format) upload#index 
         POST /upload(.:format) upload#create 
    
  • cd \Users\pratik\RubymineProjects\upload

  • rake routes
  • 很多線路,這東西應該像對中你會看到(你的輸出可能會有所不同)請注意出現的名稱(在我的示例中,它的「上傳」可能會在您的輸出中有所不同)

  • 然後最後在您的視圖form_tag中最後使用此名稱,前提是_path它,所以你應該有這樣的事情:<%= form_tag upload_path do %>
+0

....我試過你的解決方案。然後發生以下錯誤 - : 路由錯誤 沒有路由匹配{:action =>「uploadFile」,:controller =>「upload」} 嘗試運行耙路由以獲取有關可用路由的更多信息。 –

+0

任何人都可以幫忙嗎? –

+0

不要急於求成,我們不會爲您提供幫助。另外,您可以使用空閒時間在Google上搜索自己的解決方案。無論如何,這個問題是另一個問題,但我仍然編輯我的答案嘗試並幫助你 – Benj

相關問題