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
-------------------------------------------
<div>
<% form_for :player, :url => { :action => :update_player, :id =>
@player.id } do |f| %>
Name: <%= f.text_field :name %> <br>
Rank: <%= f.text_field :rank %> <br>
<% f.fields_for :racquets do |racquets_form| %>
Racquet Name: <%= racquets_form.text_field :typ %> <br>
<% end %>
<%= f.submit "Submit" %>
<% end %>
</div>
--------------------------------------------------------------------------------
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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''). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.
On Mar 27, 10:16 pm, slindsey3000
<slindsey3...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
> -sOKidTBha9n8wqgLsPFqD56Ply192/tZ@public.gmane.org
> end
>
> def update_player
> @player = Player.new(params[:player])
> -lYj0gqx2gZPfhZSSa7DgLw@public.gmane.org
> redirect_to :action => :index
> end
>
> end
>
> ----------------------- VIEW
> -------------------------------------------
>
> <div>
> <% form_for :player, :url => { :action => :update_player, :id
=>
> @player.id } do |f| %>
>
> Name: <%= f.text_field :name %> <br>
> Rank: <%= f.text_field :rank %> <br>
>
> <% f.fields_for :racquets do |racquets_form| %>
>
> Racquet Name: <%= racquets_form.text_field :typ %> <br>
> <% end %>
>
> <%= f.submit "Submit" %>
> <% end %>
> </div>
>
> ---------------------------------------------------------------------------
-----
>
> 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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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! On Mar 28, 6:08 am, Art Shayderovtry <ashayde...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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'').-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.