PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > File Uploading codes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
File Uploading codes

Réponse
 
LinkBack Outils de la discussion
Vieux 13/11/2007, 12h01   #1
Nadeesha Meththananda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/.

  Réponse avec citation
Vieux 13/11/2007, 12h19   #2
Alex Young
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Uploading codes

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! :-)

--
Alex

  Réponse avec citation
Vieux 13/11/2007, 12h37   #3
Nadeesha Meththananda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Uploading codes



Hi Alex Young

Thanks a lot for the advice.....

thanks a lot


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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h24.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,28404 seconds with 11 queries