Paul McGuane
2012-Jun-09 02:36 UTC
ActiveRecord::AssociationTypeMismatch in UsersController#update
Hi
Im trying to link my User model to a Teams model. I seem to be getting
the following errors. Any help would be great as Im just new to RoR
Thanks
error
ActiveRecord::AssociationTypeMismatch in UsersController#update
Team(#2183395560) expected, got String(#2174675960)
app/controllers/users_controller.rb:67:in `update''
app/controllers/users_controller.rb:66:in `update''
users_controller.rb
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(users_url,
:notice => "The information for #{@user.first_name}
#{@user.last_name} was successfully updated.") }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @user.errors, :status =>
:unprocessable_entity }
end
end
end
user.rb
class User < ActiveRecord::Base
belongs_to :team
attr_accessible :team
team.rb
class Team < ActiveRecord::Base
has_many :users
has_many :schedules
attr_accessible :name
end
user/_form.html.erb
<div class="_25">
<p>
<%= f.label :team %>
<%= select("user", "team", Team.all.collect
{ |p| [p.name,
p.id] }, {:include_blank => ''None''}) %>
</p>
</div>
--
Posted via http://www.ruby-forum.com/.
--
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.
Colin Law
2012-Jun-09 07:51 UTC
Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
On 9 June 2012 03:36, Paul McGuane <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > Im trying to link my User model to a Teams model. I seem to be getting > the following errors. Any help would be great as Im just new to RoR > Thanks > > error > ActiveRecord::AssociationTypeMismatch in UsersController#update > > Team(#2183395560) expected, got String(#2174675960) > app/controllers/users_controller.rb:67:in `update'' > app/controllers/users_controller.rb:66:in `update''Perhaps it would be useful if you told us which is line 66.> > > users_controller.rb > # PUT /users/1 > # PUT /users/1.json > def update > @user = User.find(params[:id]) > > respond_to do |format| > if @user.update_attributes(params[:user])In case I have made a lucky guess and it is this line then look in development.log to see what params[:user] will be. Also have a look at the Rails Guide on debugging to see techniques that can be used to debug your code. Colin> format.html { redirect_to(users_url, > :notice => "The information for #{@user.first_name} > #{@user.last_name} was successfully updated.") } > format.json { head :no_content } > else > format.html { render :action => "edit" } > format.json { render :json => @user.errors, :status => > :unprocessable_entity } > end > end > end > > > user.rb > class User < ActiveRecord::Base > belongs_to :team > attr_accessible :team > > > team.rb > class Team < ActiveRecord::Base > has_many :users > has_many :schedules > attr_accessible :name > end > > > user/_form.html.erb > <div class="_25"> > <p> > <%= f.label :team %> > <%= select("user", "team", Team.all.collect { |p| [p.name, > p.id] }, {:include_blank => ''None''}) %> > </p> > </div> > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. >-- 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.
Андрей Большов
2012-Jun-09 14:40 UTC
Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html
I gues it''s raise because you don''t assign object. You should
change it
this way:
<%= select("user", "*team_id*", Team.all.collect { |p|
[p.name,
p.id] }, {:include_blank => ''None''}) %>
суббота, 9 июня 2012 г., 6:36:36 UTC+4 пользователь Ruby-Forum.com User
написал:>
> Hi
> Im trying to link my User model to a Teams model. I seem to be getting
> the following errors. Any help would be great as Im just new to RoR
> Thanks
>
> error
> ActiveRecord::AssociationTypeMismatch in UsersController#update
>
> Team(#2183395560) expected, got String(#2174675960)
> app/controllers/users_controller.rb:67:in `update''
> app/controllers/users_controller.rb:66:in `update''
>
>
> users_controller.rb
> # PUT /users/1
> # PUT /users/1.json
> def update
> @user = User.find(params[:id])
>
> respond_to do |format|
> if @user.update_attributes(params[:user])
> format.html { redirect_to(users_url,
> :notice => "The information for #{@user.first_name}
> #{@user.last_name} was successfully updated.") }
> format.json { head :no_content }
> else
> format.html { render :action => "edit" }
> format.json { render :json => @user.errors, :status =>
> :unprocessable_entity }
> end
> end
> end
>
>
> user.rb
> class User < ActiveRecord::Base
> belongs_to :team
> attr_accessible :team
>
>
> team.rb
> class Team < ActiveRecord::Base
> has_many :users
> has_many :schedules
> attr_accessible :name
> end
>
>
> user/_form.html.erb
> <div class="_25">
> <p>
> <%= f.label :team %>
> <%= select("user", "team",
Team.all.collect { |p| [p.name,
> p.id] }, {:include_blank => ''None''}) %>
> </p>
> </div>
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/IAT4Dkdj6v0J.
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.
Paul McGuane
2012-Jun-09 14:47 UTC
Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
Hi
Thanks for the help, i managed to get this to work with the following
line
<%= f.select("team_id", Team.all.collect { |p| [p.name, p.id] }, {
:include_blank => ''None'' }) %>
though in my view/users/index
when i call the following <%= user.team %> i am getting this displayed
#<Team:0x10532a4f0>, i want it to display what team the player has been
allocated to
in my view/teams/show if i call the following
<% for player in @team.users %><%= player.first_name %> <%=
player.last_name %>
i can see what players are loaded for a particular team
thanks
--
Posted via http://www.ruby-forum.com/.
--
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.
Hassan Schroeder
2012-Jun-09 14:57 UTC
Re: Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
On Sat, Jun 9, 2012 at 7:47 AM, Paul McGuane <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> though in my view/users/index > when i call the following <%= user.team %> i am getting this displayed > #<Team:0x10532a4f0>, i want it to display what team the player has been > allocated toThat (#<Team:0x10532a4f0>) *is* the team - a Team *object*. Maybe you actually want to display an *attribute* of the object? Like says its name? Look at your own ''player'' example. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Paul McGuane
2012-Jun-09 15:05 UTC
Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
hi hassan i have tried something similar and got this NoMethodError in Users#index Showing /Users/*****/Sites/rails_projects/*****/app/views/users/index.html.erb where line #67 raised: undefined method `teams'' for nil:NilClass Extracted source (around line #67): 64: <td 65: <% if session[:user_id] == user.id %> class="active_cell" 66: <% end %>> 67: <% for team in @user.teams 68: team.name 69: end %> 70: -- Posted via http://www.ruby-forum.com/. -- 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.
Hassan Schroeder
2012-Jun-09 15:10 UTC
Re: Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
On Sat, Jun 9, 2012 at 8:05 AM, Paul McGuane <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> i have tried something similar and got this? That''s not remotely similar to your original example:> when i call the following <%= user.team %> i am getting this displayedDoesn''t it seem apparent that if this <%= user.team %> shows you the team object, <%= user.team.name %> would show you the e.g. name attribute of that object? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Paul McGuane
2012-Jun-09 15:15 UTC
Re: ActiveRecord::AssociationTypeMismatch in UsersController#update
Thanks Hassan, I now have a better understanding of what you mean -- Posted via http://www.ruby-forum.com/. -- 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.
Seemingly Similar Threads
- Forms: handle foreign keys (AssociationTypeMismatch)
- ActiveRecord::AssociationTypeMismatch (User(#54754560) expected, got User(#54510280))
- AssociationTypeMismatch with RSpec 1.0.8, autotest running with drb
- Devise AssociationTypeMismatch
- what;s the right url in this case