2013-07-24 36 views
0

我建立的紅寶石礦。但下面的RoR應用程序似乎並不運行`爲什麼form_ tag代碼在ruby mine 5.4.3.2.1中不起作用?

<%= 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 

我一直沒有找到解決辦法。我一直在嘗試一個星期。

+0

如何[Rü保存文件的應用程序?最好使用寶石回形針。 – Debadatt

回答

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

+0

沒有工作...... –

+0

你可以發佈任何中斷時Rails服務器顯示的輸出。 – robinjohnobrien

相關問題