File Uploading codes

I used the following codes found in the net to use to write a file to
upload the files. down are the codes i have used. but still i did not
able to get the correct coding. so if somebody know where i have gone
wrong pls reply.

###################### IN the View #############################3
<form action="create" method="post" enctype="multipart/form-data">
  <p>
    <b>Name:</b><br />
    <%= text_field "person", "name" %>
  </p>

  <p>
    <b>Picture:</b><br />
    <input type="file" name="person[picture]" />
  </p>

  <p><input type="submit" name="Save" /></p>
</form>

#################The controller:##########################

class AddressbookController < ApplicationController
  def new
    # not really needed since the template doesn't rely on any data
  end

  def create
    post = Post.save(@params["person"])
    # Doesn't this mean post is a File object?
    # post.id is a bad idea in this case...

    redirect_to :action => "show", :id => post.id
  end
end

######### the model ###################

class Post < ActiveRecord::Base
  def self.save(person)
    f = File.new("pictures/#{person['name']}/picture.jpg", "wb")
    f.write params[:picture].read
    f.close
end

in the uploading it gives a error like this

undefined local variable or method `params' for Post:Class

so i did some modification as
f.write params[:picture].read
        line to
f.write (@params[:picture].read)

so the code woke properly, but still it gives a error saying

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

SO IF SOMEBODY KNOW HOW TO OVER COME THIS PROBLEM PLS REPLY

···

--
Posted via http://www.ruby-forum.com/.

Nadeesha Meththananda wrote:
<snip>

######### the model ###################

class Post < ActiveRecord::Base
  def self.save(person)
    f = File.new("pictures/#{person['name']}/picture.jpg", "wb")
    f.write params[:picture].read

You'll probably get a better response over on the Rails list, but I suspect this is your problem - the params hash isn't in scope in ActiveRecord descendants. You should have more luck if you define your method as def self.save(person, picture), and call it in you controller as Post.save(params[:person], params[:picture]).

<snip>

SO IF SOMEBODY KNOW HOW TO OVER COME THIS PROBLEM PLS REPLY

NO NEED TO SHOUT! :slight_smile:

···

--
Alex

Hi Alex Young

Thanks a lot for the advice.....

thanks a lot

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.