Rails文件上传

时间:2014-04-15 20:51:17

标签: ruby-on-rails ruby

我一直在努力解决此错误,以便在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

这是我的VIew

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

3 个答案:

答案 0 :(得分:1)

1)您的表单应设置为multipart以便能够处理文件:

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

2)由于您的文件上传字段位于表单内,因此您需要使用以下命令访问该值:

params[:tutorial][:upload]

3)检查堆栈跟踪并尝试查找对name方法的调用发生的位置,因为您似乎正在尝试在空对象上调用该方法。

希望这有帮助

答案 1 :(得分:1)

其他答案很有用;另外,您不应该覆盖Tutorial.save方法,因为saveRecord定义了save方法,以便将模型实际保存在数据库中;使用before_save或其他回调

答案 2 :(得分:0)

了解回形针
https://github.com/thoughtbot/paperclip
很不错

如果您希望让代码正常工作,请更改params变量。 按原样看

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

应该是

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