我一直在努力解決這個錯誤上傳文件在軌道上 這裏是我的代碼....我是一個初學者,請幫助,,我給下面我的模型,控制器和視圖..下面給出的是我的代碼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
爲什麼你不嘗試載波或paratrail寶石。他們提供相當先進的功能。 –
我不能,我不能使用任何其他的寶石.. – pritesh