# Using attachment\_fu in existing user model with edit/update

**URL:** <https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764>\
**Category:** rubyonrails-talk\
**Created:** [August 29, 2008, 3:45am UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764 "2008-08-29T03:45:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 29, 2008, 3:45am UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/1 "2008-08-29T03:45:08Z")

</div>

Hi all,

I currently have a working development site with the user model already setup. I did however hold out on user images/avatars until the end and am now thinking it wasn't such a good idea.

I'm using attachment\_fu with imagescience to do the uploads. I have installed everything and setup a test scaffold following the screencast at [http://www.rubyplus.org/episodes/31-How-to-upload-images-in-Rails-2-.html](http://www.rubyplus.org/episodes/31-How-to-upload-images-in-Rails-2-.html) and got that up and running without a problem.

My issue is this:

I have a user model which when a user registers (new/create action) I want to just set his account up with the least amount of information (username, email, and password). I don't want the user to worry about uploading the image at the time of registration. I've been trying to set up the code so that the user can upload the image when editing his/her profile but I'm getting different errors even though I'm following the same logic as the screencast I previously mentioned (same exact editing code actually). I've also added the necessary fields for the database fields to the user table. But I've had no luck.

Does attachment\_fu only work if you upload the images at the time of using the new/create actions? There must be a way to make this work by just editing a user. I'm sure I'm just overlooking something, any help is appreciated!!

Let me know if there is something else you need to know. Thank you for your time!

Best regards, Tony

---

<div class="post-metadata">

**Author:** ![Jodi1](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@Jodi1](https://discuss.rubyonrails.org/u/Jodi1)\
**Post date:** [August 29, 2008, 3:50am UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/2 "2008-08-29T03:50:57Z")

</div>

Tony -

> Hi all,
> 
> I currently have a working development site with the user model already setup. I did however hold out on user images/avatars until the end and am now thinking it wasn't such a good idea.
> 
> I'm using attachment\_fu with imagescience to do the uploads. I have installed everything and setup a test scaffold following the screencast at [Join Collage](http://www.rubyplus.org/episodes/31-How-to-upload-images-in-Rails-2-.html) and got that up and running without a problem.
> 
> My issue is this:
> 
> I have a user model which when a user registers (new/create action) I want to just set his account up with the least amount of information (username, email, and password). I don't want the user to worry about uploading the image at the time of registration. I've been trying to set up the code so that the user can upload the image when editing his/her profile but I'm getting different errors even though I'm following the same logic as the screencast I previously mentioned (same exact editing code actually). I've also added the necessary fields for the database fields to the user table. But I've had no luck.
> 
> Does attachment\_fu only work if you upload the images at the time of using the new/create actions? There must be a way to make this work by just editing a user. I'm sure I'm just overlooking something, any help is appreciated!!
> 
> Let me know if there is something else you need to know. Thank you for your time!

best to place your images in a separate model.

has\_many :images

but best if images has a polymorphic relationship - that way you can add images to other models

Class User \< ActiveRecord::Base

&nbsp;&nbsp;&nbsp;has\_many :images, :as =\> :attacheable, :dependent =\> :destroy

end

Class Image \< ActiveRecord::Base

&nbsp;&nbsp;&nbsp;belongs\_to :attacheable, :polymorphic =\> true &nbsp;&nbsp;&nbsp;has\_attachment :storage =\> :file\_system, ...

end

Jodi

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 29, 2008, 12:40pm UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/3 "2008-08-29T12:40:29Z")

</div>

Jodi Showers wrote:

> Tony -
> 
> > installed everything and setup a test scaffold following the  
> > uploading the image at the time of registration. I've been trying to  
> > just editing a user. I'm sure I'm just overlooking something, any help is appreciated!!
> > 
> > Let me know if there is something else you need to know. Thank you for your time!
> 
> best to place your images in a separate model.
> 
> has\_many :images
> 
> but best if images has a polymorphic relationship - that way you can add images to other models
> 
> Class User \< ActiveRecord::Base
> 
> &nbsp;&nbsp;&nbsp;has\_many :images, :as =\> :attacheable, :dependent =\> :destroy
> 
> end
> 
> Class Image \< ActiveRecord::Base
> 
> &nbsp;&nbsp;&nbsp;belongs\_to :attacheable, :polymorphic =\> true &nbsp;&nbsp;&nbsp;has\_attachment :storage =\> :file\_system, ...
> 
> end
> 
> Jodi

Hi Jodi,

Thanks for the reply. I tried adding the additional table relationships as you suggested but I'm getting an error on the user update action: undefined local variable or method `filename' for #\<User:0x2564508\>

I have the standard update code in my controller:

def update &nbsp;&nbsp;&nbsp;&nbsp;@user = User.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @user.update\_attributes(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'User was successfully updated.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@user) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "edit" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @user.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and the view code for the upload is: \<%= file\_field\_tag :uploaded\_data %\>

Any help is appreciated! Thanks.

-Tony

---

<div class="post-metadata">

**Author:** ![Dirk\_Groten](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dirk\_Groten](https://discuss.rubyonrails.org/u/Dirk_Groten)\
**Post date:** [August 29, 2008, 3:23pm UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/4 "2008-08-29T15:23:27Z")

</div>

Hi Tony, The problem is you're using the user's form and including attributes of another model in it. I guess your params look like 'user' =\> { 'name' =\> 'something', ..., 'filename' =\> 'image.jpg'}, but filename is not an attribute of User model but of Image model.

Jodi's tip of creating a separate model has more implications than just setting up the relationship: You should create a separate controller for creating the image: In your user's form, surely when he wants to add an image, he gets to an image upload view. This would call a create action in the ImageController: def create # in ImageController, not UserController &nbsp;&nbsp;@user = current\_user # or User.find(params[:user\_id]) if you post the user\_id in the form for the image. &nbsp;&nbsp;@image = Image.new(params[:image]) #this is where the filename attribute gets set &nbsp;&nbsp;if @image.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.image \<\< @image#associate image to the user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;end end

If you prefer to do the association only when the user saves his profile, you could just pass the image\_id back to the form for the user's profile and then add the association with the UserController update action. As image\_id is an attribute of user, you can just mass- assign it like you show in @user.update\_attributes(params[:user]).

---

<div class="post-metadata">

**Author:** ![Jodi1](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@Jodi1](https://discuss.rubyonrails.org/u/Jodi1)\
**Post date:** [August 29, 2008, 3:46pm UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/5 "2008-08-29T15:46:37Z")

</div>

Thanx for jumping in Degee -

> Hi Tony, The problem is you're using the user's form and including attributes of another model in it. I guess your params look like 'user' =\> { 'name' =\> 'something', ..., 'filename' =\> 'image.jpg'}, but filename is not an attribute of User model but of Image model.
> 
> Jodi's tip of creating a separate model has more implications than just setting up the relationship: You should create a separate controller for creating the image: In your user's form, surely when he wants to add an image, he gets to an image upload view. This would call a create action in the ImageController: def create # in ImageController, not UserController @user = current\_user # or User.find(params[:user\_id]) if you post the user\_id in the form for the image. @image = Image.new(params[:image]) #this is where the filename attribute gets set if @image.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.image \<\< @image#associate image to the user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end end
> 
> If you prefer to do the association only when the user saves his profile, you could just pass the image\_id back to the form for the user's profile and then add the association with the UserController update action. As image\_id is an attribute of user, you can just mass- assign it like you show in @user.update\_attributes(params[:user]).
> 
> > Jodi Showers wrote:
> > 
> > > Tony -
> > 
> > > > installed everything and setup a test scaffold following the uploading the image at the time of registration. I've been trying to just editing a user. I'm sure I'm just overlooking something, any help is appreciated!!
> > 
> > > > Let me know if there is something else you need to know. Thank you for your time!
> > 
> > > best to place your images in a separate model.
> > 
> > > has\_many :images
> > 
> > > but best if images has a polymorphic relationship - that way you can add images to other models
> > 
> > > Class User \< ActiveRecord::Base
> > 
> > > &nbsp;&nbsp;&nbsp;has\_many :images, :as =\> :attacheable, :dependent =\> :destroy
> > 
> > > end
> > 
> > > Class Image \< ActiveRecord::Base
> > 
> > > &nbsp;&nbsp;&nbsp;belongs\_to :attacheable, :polymorphic =\> true &nbsp;&nbsp;&nbsp;has\_attachment :storage =\> :file\_system, ...
> > 
> > > end
> > 
> > > Jodi
> > 
> > Hi Jodi,
> > 
> > Thanks for the reply. I tried adding the additional table relationships as you suggested but I'm getting an error on the user update action: undefined local variable or method `filename' for #\<User:0x2564508\>
> > 
> > I have the standard update code in my controller:
> > 
> > def update &nbsp;&nbsp;&nbsp;&nbsp;@user = User.find(params[:id])
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @user.update\_attributes(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'User was successfully updated.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@user) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "edit" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @user.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> > 
> > and the view code for the upload is: \<%= file\_field\_tag :uploaded\_data %\>
> > 
> > Any help is appreciated! Thanks.
> > 
> > -Tony -- Posted viahttp://www.ruby-forum.com/.

I prefer to create images within the (in this case) the UserController.

ripped out of a project

View - \<multipart form tag here\>

\<%= file\_field\_tag "photo", :id =\> "photo\_image", :class =\> "attach\_photo" %\> \<%= text\_field\_tag "caption", nil, {:id =\> "photo\_title", :class =\> "attach\_photo"} %\>

User controller - after @user object is valid + saved

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#attached uploaded pics - an array of photos + captions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:photo] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:photo].each\_with\_index do |image, caption\_index| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caption = '' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caption = params[:caption][caption\_index] if params[:caption] && params[:caption][caption\_index]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.images.create!(:uploaded\_data =\> image, :caption =\> caption) if image.size != 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Jodi

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 3, 2008, 3:24am UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/6 "2008-09-03T03:24:20Z")

</div>

Jodi Showers wrote:

> Thanx for jumping in Degee -
> 
> > wants to add an image, he gets to an image upload view. This would &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
> > 
> > > > > is appreciated!!
> > > > 
> > > > add images to other models &nbsp;&nbsp;&nbsp;has\_attachment :storage =\> :file\_system, ...
> > > 
> > > undefined local variable or method `filename' for #\<User:0x2564508\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok }
> > > 
> > > Any help is appreciated! Thanks.
> > > 
> > > -Tony -- Posted viahttp://www.ruby-forum.com/.
> 
> I prefer to create images within the (in this case) the UserController.
> 
> ripped out of a project
> 
> View - \<multipart form tag here\>
> 
> \<%= file\_field\_tag "photo", :id =\> "photo\_image", :class =\> "attach\_photo" %\> \<%= text\_field\_tag "caption", nil, {:id =\> "photo\_title", :class =\> "attach\_photo"} %\>
> 
> User controller - after @user object is valid + saved
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#attached uploaded pics - an array of photos + captions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:photo] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params[:photo].each\_with\_index do |image, caption\_index| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caption = '' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caption = params[:caption][caption\_index] if params[:caption] && params[:caption][caption\_index]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.images.create!(:uploaded\_data =\> image, :caption =\> caption) if image.size != 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> Jodi

Thanks for all the help guys.

I finally got back to work after being busy from my day job. I got the filename situation cleared but now I get the following error:

undefined method `content\_type' for [#\<ActionController::UploadedStringIO:0x23f8e1c\>, 0]:Array

I'm doing things the "Jodi way". Maybe I'm just tired and can't see the obvious solution but any advice is really appreciated as always.

And yes, I realize that this info is being spoon fed to me and I STILL can't get it. Trust me, it's more frustrating to me than it is to anyone else! :-\

Thanks again all!!!

-Tony

---

<div class="post-metadata">

**Author:** ![Dirk\_Groten](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dirk\_Groten](https://discuss.rubyonrails.org/u/Dirk_Groten)\
**Post date:** [September 8, 2008, 10:13am UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/7 "2008-09-08T10:13:48Z")

</div>

It's a bit difficult to help you without the code here. Looks like you're not performing the correct assignment in your controller. content\_type is a method that can only be called on a UploadedStringIO object and somehow you're typecasting it to Array it seems.

Please show us the code for your controller update action and your edit view. And it also helps to tell us which params are being passed when you call the action (from your development.log).

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 26, 2010, 6:39pm UTC](https://discuss.rubyonrails.org/t/using-attachment-fu-in-existing-user-model-with-edit-update/33764/8 "2010-03-26T18:39:11Z")

</div>

> Hi Jodi,
> 
> Thanks for the reply. I tried adding the additional table relationships as you suggested but I'm getting an error on the user update action: undefined local variable or method `filename' for #\<User:0x2564508\>
> 
> I have the standard update code in my controller:
> 
> def update &nbsp;&nbsp;&nbsp;&nbsp;@user = User.find(params[:id])
> 
> &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @user.update\_attributes(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'User was successfully updated.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@user) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "edit" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @user.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> and the view code for the upload is: \<%= file\_field\_tag :uploaded\_data %\>
> 
> Any help is appreciated! Thanks.
> 
> -Tony

I know this is an old post, but I was trying to find the solution to: undefined local variable or method `filename' and I arrived here.

I got this error because I made a mistake creating the table. I used "file\_name" instead of "filename". Changing this solved the problem for me.

Cheers Aukan
