collection_select给ActiveRecord :: AssociationTypeMismatch [英] collection_select giving ActiveRecord::AssociationTypeMismatch

查看:95
本文介绍了collection_select给ActiveRecord :: AssociationTypeMismatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确地提出这个问题,或者说太累了。



我的代码是:



_form.html.haml

  = f.collection_select:category,Category.where (:user_id => current_user.id),:id,:name 

/ p>

 链接控制器中的ActiveRecord :: AssociationTypeMismatch#create 
类别(#70203963939880)expected,got String(#70203929255820)

日志是

 启动POST/ links为127.0.0.1在2012-11-12 22:14:05 -0800 
处理LinksController#create as HTML
参数:{utf8=> ✓,authenticity_token=>z4RYcUW3hGLfKBAwQLMZbye2I1mn16fg6BSBGe7GILU =,link=> {name=>SFGate,url=>http://www.sfgate.com/ ,category=>1},commit=>保存}
已完成500内部服务器错误:0ms

看起来好像它的传递:id作为字符串而不是整数。任何线索?



更新:

  ActiveRecord :: Base 
belongs_to:user
has_many:links

attr_accessible:name,:color,:position

COLOR_CODES = [Green Blue,Yellow,Orange,Red,Purple,Pink]
end

class Link< ActiveRecord :: Base
belongs_to:user
belongs_to:category

attr_accessible:category,:name,:position,:url,:user_id
end

修正:我有

  t.integer:category 

而不是

  t.integer:category_id 

和它的工作完美 - 我想我太累了)。

解决方案

你应该改变:

  f.collection_select:category,Category.where(:user_id => current_user.id),:id,:name 



到:

  f.collection_select: category_id,Category.where(:user_id => current_user.id),:id,:name 


I'm not sure if I'm asking this question right or am just too tired for it to be obvious.

My code is:

_form.html.haml

 = f.collection_select :category, Category.where(:user_id => current_user.id), :id, :name

What I get is

 ActiveRecord::AssociationTypeMismatch in LinksController#create
 Category(#70203963939880) expected, got String(#70203929255820)

Log is

 Started POST "/links" for 127.0.0.1 at 2012-11-12 22:14:05 -0800
 Processing by LinksController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"z4RYcUW3hGLfKBAwQLMZbye2I1mn16fg6BSBGe7GILU=", "link"=>{"name"=>"SFGate", "url"=>"http://www.sfgate.com/", "category"=>"1"}, "commit"=>"Save"}
 Completed 500 Internal Server Error in 0ms

It appears as though its passing :id as a string instead of an integer. Any clues?

Update:

class Category < ActiveRecord::Base
  belongs_to :user
  has_many :links

  attr_accessible :name, :color, :position

  COLOR_CODES = ["Green", "Blue", "Yellow", "Orange", "Red", "Purple", "Pink"]
end

class Link < ActiveRecord::Base
  belongs_to :user
  belongs_to :category

  attr_accessible :category, :name, :position, :url, :user_id
end

Fixed: I had

 t.integer :category 

instead of

 t.integer :category_id 

switched everything over and its working perfect - I guess I was too tired :)

解决方案

You should change:

f.collection_select :category, Category.where(:user_id => current_user.id), :id, :name

to:

f.collection_select :category_id, Category.where(:user_id => current_user.id), :id, :name

这篇关于collection_select给ActiveRecord :: AssociationTypeMismatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆