# Accepts\_nested\_attributes\_for - issue

**URL:** <https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878>\
**Category:** Rails\
**Created:** [March 28, 2010, 7:19am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878 "2010-03-28T07:19:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [March 28, 2010, 7:19am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878/1 "2010-03-28T07:19:19Z")

</div>

Just trying a simple example to try to get nested to work… but can’t  
get it! Been 2 days! Please help. VERY SIMPLE controller, view,  
models… listed below.

----------------------ERROR  
MESSAGE----------------------------------------------------------------------

ActiveRecord::AssociationTypeMismatch in UserController#update\_player

Racquet(#38866740) expected, got Array(#20762670)

* * *

-----------------MODELS-------------------------------------

class Player \< ActiveRecord::Base

```
has_many :racquets, :dependent => :destroy

accepts_nested_attributes_for :racquets

```

end

class Racquet \< ActiveRecord::Base

```
 belongs_to :player

```

end

--------------CONTROLLER-----------------------------

class UserController \< ApplicationController

def index  
@player = Player.new  
@player.racquets.build  
end

def update\_player  
@player = Player.new(params[:player])  
@player.save  
redirect\_to :action =\> :index  
end

end

## ----------------------- VIEW

\<% form\_for :player, :url =\> { :action =\> :update\_player, :id =\> @player.id } do |f| %\>

Name: \<%= f.text\_field :name %\>   
  
Rank: \<%= f.text\_field :rank %\>

\<% f.fields\_for :racquets do |racquets\_form| %\>

Racquet Name: \<%= racquets\_form.text\_field :typ %\>   
  
\<% end %\>

\<%= f.submit “Submit” %\>  
\<% end %\>

* * *

This was supposed to be quick practice form to make use of nested  
models… and has turned into a nightmare. Can anyone tell my why it  
is not working???

Thanks so much,

Shawn

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [March 28, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878/2 "2010-03-28T17:55:59Z")

</div>

I haven’t tried running your code, but please change:

def update\_player  
@player = Player.new(params[:player])  
@player.save  
redirect\_to :action =\> :index  
end

to

def update\_player  
@player = Player.find\_by\_id(params[:id])  
@player.update\_attributes(params[:player])  
redirect\_to :action =\> :index  
end

and give it a shot.

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [March 28, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878/3 "2010-03-28T18:35:12Z")

</div>

It’s rather unusual to name the action ‘index’ when it shows form for  
creating a new object. Why not call it ‘new’ or something like that.  
And then the action is named ‘update\_player’ when in fact it is  
creating a new Player object. Why don’t you name it ‘create’?  
I’m sorry for nitpicking but choosing the right names will make your  
life easier. (and will raise your chances for getting the answer)

Regarding your question. I usually look at params in such a situation.  
You can find request params in webserver log(just scroll console  
window where webrick is started) or you can insert a call to  
‘debugger’ in the action and start webserver in debug mode (with ‘–  
debugger’).

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [March 29, 2010, 9:29am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878/4 "2010-03-29T09:29:29Z")

</div>

It really helps if you post as much information as possible. In this  
particular case your form html (I mean from page source in browser)  
and request params would be very helpful. I’m not an expert in nested  
forms but it’s probably because you are receiving an array instead of  
a hash in a request.

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [March 28, 2010, 8:25pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-issue/185878/5 "2010-03-28T20:25:50Z")

</div>

Yes, you are correct. Terrible naming. But was done to try quick  
example. I am creating new player so  
@cpr it won’t find player because I am in fact creating one. I  
know… terrible code.

It seems to be looking for Active Record object and I am passing in  
array…

Help!
