2014-04-15 87 views
1

我一直在努力解決這個錯誤上傳文件在軌道上 這裏是我的代碼....我是一個初學者,請幫助,,我給下面我的模型,控制器和視圖..下面給出的是我的代碼Rails文件上傳

class Tutorial < ActiveRecord::Base 

    def self.save(upload) 

    name = upload['upload'].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['upload'].read) } 
    end 

    attr_accessible :category_id, :tutorial_date_release, :tutorial_discription, :tutorial_name, :tutorial_path, :tutorial_teacher_name, :tutorial_type, :upload 

    belongs_to :category 

    validates_presence_of :category_id 

    validates_presence_of :tutorial_date_release 

    validates_presence_of :tutorial_discription 

    validates_presence_of :tutorial_name 

    validates_presence_of :tutorial_path 

    validates_presence_of :tutorial_teacher_name 

    validates_presence_of :tutorial_type 

    validates_presence_of :tutorial_type 

    #validates_presence_of :upload 

    #validates_attachment_content_type upload, :content_type => 'application/pdf' 

end 

這裏是我的控制器

class TutorialsController < ApplicationController 
    # GET /tutorials 
    # GET /tutorials.json 
    def index 
    @tutorials = Tutorial.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @tutorials } 
    end 

    end 
def uploadFile 
    post = Tutorial.save(params[:upload]) 
    render :text => "File has been uploaded successfully" 
    end 
    # GET /tutorials/1 
    # GET /tutorials/1.json 
    def show 
    @tutorial = Tutorial.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @tutorial } 
    end 
    end 

    # GET /tutorials/new 
    # GET /tutorials/new.json 
    def new 
    @tutorial = Tutorial.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @tutorial } 
    end 
    end 

    # GET /tutorials/1/edit 
    def edit 
    @tutorial = Tutorial.find(params[:id]) 
    end 

    # POST /tutorials 
    # POST /tutorials.json 
    def create 
    @tutorial = Tutorial.new(params[:tutorial]) 

    respond_to do |format| 
     if @tutorial.save 
     format.html { redirect_to @tutorial, notice: 'Tutorial was successfully created.' } 
     format.json { render json: @tutorial, status: :created, location: @tutorial } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @tutorial.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /tutorials/1 
    # PUT /tutorials/1.json 
    def update 
    @tutorial = Tutorial.find(params[:id]) 

    respond_to do |format| 
     if @tutorial.update_attributes(params[:tutorial]) 
     format.html { redirect_to @tutorial, notice: 'Tutorial was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @tutorial.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /tutorials/1 
    # DELETE /tutorials/1.json 
    def destroy 
    @tutorial = Tutorial.find(params[:id]) 
    @tutorial.destroy 

    respond_to do |format| 
     format.html { redirect_to tutorials_url } 
     format.json { head :no_content } 
    end 
    end 
end 

這是我的看法

<%= form_for(@tutorial) do |f| %> 
    <% if @tutorial.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@tutorial.errors.count, "error") %> prohibited this tutorial from being saved:</h2> 

     <ul> 
     <% @tutorial.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :tutorial_name %><br /> 
    <%= f.text_field :tutorial_name %> 
    </div> 
    <div class="field"> 
    <%= f.label :tutorial_type %><br /> 
    <%= f.text_field :tutorial_type %> 
    </div> 
    <div class="field"> 
    <%= f.label :tutorial_date_release %><br /> 
    <%= f.date_select :tutorial_date_release %> 
    </div> 
    <div class="field"> 
    <%= f.label :tutorial_teacher_name %><br /> 
    <%= f.text_area :tutorial_teacher_name %> 
    </div> 
    <div class="field"> 
    <%= f.label :tutorial_discription %><br /> 
    <%= f.text_area :tutorial_discription %> 
    </div> 
    <div class="field"> 
    <%= f.label :tutorial_path %><br /> 
    <%= f.text_field :tutorial_path %> 
    </div> 
    <div class="field"> 
    <%= f.label :category_id %><br /> 
    <%= f.number_field :category_id %> 
    </div> 
    <div class="field"> 
    <%= f.file_field :upload %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

每當我提出我有一樣的錯誤:

undefined method `name' for nil:NilClass 
Rails.root: C:/Users/Pritesh/Desktop/WebTutor-master/WebTutor-master 

Application Trace | Framework Trace | Full Trace 
app/controllers/tutorials_controller.rb:50:in `block in create' 
app/controllers/tutorials_controller.rb:49:in `create' 
Request 
+0

爲什麼你不嘗試載波或paratrail寶石。他們提供相當先進的功能。 –

+0

我不能,我不能使用任何其他的寶石.. – pritesh

回答

1

1)你的形式應設置爲多,能夠進行文件操作:

form_for(@tutorial, :html => { :multipart => true })

2)由於您的文件上傳字段在表單內,您需要訪問該值:

params[:tutorial][:upload]

3)檢查你的堆棧跟蹤,並試圖找到name方法的調用發生在哪裏,因爲你似乎試圖在空對象上調用該方法。

希望這會有幫助

0

閱讀有關回形針
https://github.com/thoughtbot/paperclip
如果你想使你的代碼的工作變化params變量很高興

。 正如看到

post = Tutorial.save(params[:upload]) 

應該

post = Tutorial.save(params[:tutorial]) 
1

其他答案可以提供很好的觀點;另外,您不應該覆蓋Tutorial.save方法,因爲save方法是由ActiveRecord定義的,以便實際將模型保存在數據庫中;使用before_save或其他回調代替