ruby - Rails file upload -


i have been struggling solve error upload file in rails here's code....i beginner, please help,, have give below model, controllver,and view..the given below code

class tutorial < activerecord::base    def self.save(upload)      name =  upload['upload'].original_filename     directory = "public/data"     # create file path     path = file.join(directory, name)     # write 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 

here controller

class tutorialscontroller < applicationcontroller   # /tutorials   # /tutorials.json   def index     @tutorials = tutorial.all      respond_to |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   # /tutorials/1   # /tutorials/1.json   def show     @tutorial = tutorial.find(params[:id])      respond_to |format|       format.html # show.html.erb       format.json { render json: @tutorial }     end   end    # /tutorials/new   # /tutorials/new.json   def new     @tutorial = tutorial.new      respond_to |format|       format.html # new.html.erb       format.json { render json: @tutorial }     end   end    # /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 |format|       if @tutorial.save         format.html { redirect_to @tutorial, notice: 'tutorial 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 |format|       if @tutorial.update_attributes(params[:tutorial])         format.html { redirect_to @tutorial, notice: 'tutorial 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 |format|       format.html { redirect_to tutorials_url }       format.json { head :no_content }     end   end end 

here view

<%= form_for(@tutorial) |f| %>   <% if @tutorial.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@tutorial.errors.count, "error") %> prohibited tutorial being saved:</h2>        <ul>       <% @tutorial.errors.full_messages.each |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 %> 

whenever submit having error like:

undefined method `name' 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 

1) form should set multipart able work files:

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

2) since file upload field inside form, need access value with:

params[:tutorial][:upload]

3) check stack trace , try find call name method happening seems trying call method on null object.

hope helps


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -