# Passing Model ID in Upload

**URL:** <https://www.ruby-forum.com/t/passing-model-id-in-upload/125847>\
**Category:** Rails\
**Created:** [January 12, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/passing-model-id-in-upload/125847 "2008-01-12T20:27:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [January 12, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/passing-model-id-in-upload/125847/1 "2008-01-12T20:27:08Z")

</div>

Greetings.

I am trying to create an image upload page for my site, and I am  
having a little trouble figuring out how to set things up correctly.  
I have a model for user’s, each with a unique ID, and I would like to  
have it so that when an image (i’m calling them artworks at the  
moment) is uploaded it is saved to /somedir//  
. The trouble is I can’t figure out how to pass  
the user ID info to rails so it will be saved in the image model. My  
code looks something like this:

# artwork migration

def self.up  
create\_table :artworks do |t|  
t.column :title, :string  
t.column :media, :string  
t.column :description, :string  
t.column :user\_id, :integer # foreign key to users table  
t.column :uploaded\_at, :datetime  
t.column :file\_path, :string  
t.column :file\_name, :string  
t.column :content\_type, :string  
end  
end

# upload template

# Upload an artwork!

\<% form\_for(:artwork,  
:url =\> { :action =\> :upload },  
:html =\> { :multipart =\> true }) do |form| %\>  
Title: \<%= form.text\_field(“title”, { :size =\> 20, :maxlength =\>  
100 }) %\>  
  
Media: \<%= form.text\_field(“media”, { :size =\> 20, :maxlength =\>  
100 }) %\>  
  
Description: \<%= form.text\_area(“description”, { :cols =\> 60, :rows  
=\> 40 })%\>  
  
Select File to Upload: \<%= form.file\_field(“uploaded\_file”) %\>

\<%= submit\_tag(“Upload”) %\>  
\<% end %\>

# controller

def upload  
logger.debug(“running upload”)

```
# Create an empty artwork to display in the form for GET/POST
logger.debug("make a new Artwork")
@artwork = Artwork.new(params[:artwork])

# If something was submitted
logger.debug("check for POST")
if request.post?
  # If the save is successful, redisplay the page with a blank

```

artwork for next submit  
logger.debug(“save to DB”)  
if @artwork.save  
@artwork = Artwork.new  
flash[:notice] = “Artwork Uploaded Successfully”  
end  
end  
end

# Model for Artwork

class Artwork \< ActiveRecord::Base  
belongs\_to :user

```
def uploaded_file=(incomming_file)
  logger.debug("running uploaded_file=")

  @temp_file = incomming_file
  @content_type = incomming_file.content_type.chomp
  @file_path = MEDIA_FOLDER + @user_id + '/' # **** error

```

here  
@file\_name = @id + “.” + @content\_type  
end

```
def after_save # called after a successful ActiveRecord save
  if @temp_file
    logger.debug("creating dir (if needed) '#{@file_path}'")
    File.makedirs @file_path
    logger.debug("saveing to file '#{@file_name}'")
    File.open("#{@file_path}#{file_name}", "wb+") do |f|
      f.write(@temp_file.read)
    end
  end
end

def after_destroy
  if File.exist?("#{@file_path}#{file_name}")
    File.delete("#{@file_path}#{file_name}")
  end
end

```

end

When I run this code, I get an error at the line in the model above  
that @user\_id is nil and can’t converted to a String. I know I need  
to somehow get the current user’s ID stored in the new artwork object,  
but I can’t figure out how. I have it stored in the session hash when  
a user is logged in. I tried putting a hidden filed in the template  
like this:

\<%= form.hidden\_field(“user\_id”, { :value =\> “#{@current\_user\_id}” })  
%\>

Where @current\_user\_id is grabs the id from the session, but same  
error.

I tried assigning the value in the controller just before calling  
@artwork = Artwork.new(params[:artwork]), but this seems to overwrite  
that value.

I tried accessing the session from within the model, but it looks like  
it’s not in scope for that code. Any help would be appreciated.

Thanks,  
Matthew
