Ruby on rails 3 未知的属性X,但X不在rails3中更新_属性的参数中
我正在尝试使用accept\u嵌套的属性在表单中设置对象的字段。但是,在控制器中,当我执行以下操作时:Ruby on rails 3 未知的属性X,但X不在rails3中更新_属性的参数中,ruby-on-rails-3,update-attributes,Ruby On Rails 3,Update Attributes,我正在尝试使用accept\u嵌套的属性在表单中设置对象的字段。但是,在控制器中,当我执行以下操作时: @device.update_attributes(params[:device]) 我得到: ActiveRecord::UnknownAttributeError "unknown attribute: device_id" 但设备id是其他非相关模型的属性,不包括在参数中。 参数如下所示 {"utf8"=>"✓", "authenticity_token"=>"Xja
@device.update_attributes(params[:device])
我得到:
ActiveRecord::UnknownAttributeError
"unknown attribute: device_id"
但设备id是其他非相关模型的属性,不包括在参数中。
参数如下所示
{"utf8"=>"✓",
"authenticity_token"=>"Xja5GCNRutpZn2c4wKeSx0KO6sNEzh09kWmPQ0/0Hys=",
"id"=>"5",
"device"=>{"routes_attributes"=>{"0"=>{"name"=>"",
"origin_attributes"=>{"name"=>"",
"lat"=>"",
"lng"=>""},
"destination_attributes"=>{"name"=>"",
"lat"=>"",
"lng"=>""}}}},
"commit"=>"Create Device"}
什么可以被认为是原因。
这是我的密码
看法
模型
class Route < ActiveRecord::Base
attr_accessible :name, :destination_attributes, :origin_attributes, :waypoints, :driver_id
has_many :waypoints
has_one :origin, :class_name=>"Origin"
has_one :destination, :class_name=>"Destination"
belongs_to :device
accepts_nested_attributes_for :origin, :destination, :waypoints
end
class Device < ActiveRecord::Base
attr_accessible :id, :name, :password
attr_accessible :device_driver_bind_attributes, :drivers_attributes, :routes_attributes, :current_location_attributes
has_many :drivers, through: :device_driver_bind
has_many :device_driver_bind, dependent: :destroy
has_one :current_location, :class_name => "CurrentLocation"
has_many :routes
has_many :origins, through: :routes
has_many :destinations, through: :routes
has_many :waypoints, through: :routes
accepts_nested_attributes_for :routes, :current_location, :device_driver_bind
end
类路由“origin”
有一个:目的地,:class\u name=>“目的地”
属于:设备
接受:起点、目的地、航路点的\u嵌套\u属性\u
结束
类设备“当前位置”
有很多路线吗
有很多:起源,经过::路线
有多个:目的地,通过::路线
有许多:航路点,通过::路线
接受以下内容的\u嵌套\u属性\u:routes、:current\u location、:device\u driver\u bind
结束
这一定是您的选择标签中的问题,请尝试以下操作:
<%= f.select(:id, options_for_select( Device.all.collect{|d| [d.name + "/" + d.get_driver().name, d.id] } ),:prompt=>"select a device") %>
“选择一个设备”)%%>
ActiveRecord::UnknownAttributeError
“未知属性:设备\u id”
看起来是因为Routes上缺少一列
ActiveRecord希望能够将此设置为
接受嵌套的\u属性\u for:routes
,以满足所属的\u to:device
这是f。选择,确切地说:=)感谢您的响应,但没有起作用。我在do_compose操作中同时更改了@device=device.find(params[:device][:id]),并得到了相同的错误。顺便说一下,在执行“@device=device.find(params[:id])时,设备对象的一些属性已经设置好了。我正试图通过@device.update_attributes(params[:device])设置其余属性。您可能希望检查路由是否缺少:device_id列。AR希望能够使用为:routes
设置此属性,以满足所属:device
的要求!这么容易犯的错误。。非常感谢你!!!我会把这变成一个完整的答案,这样你就可以把它标记为已解决。
class Route < ActiveRecord::Base
attr_accessible :name, :destination_attributes, :origin_attributes, :waypoints, :driver_id
has_many :waypoints
has_one :origin, :class_name=>"Origin"
has_one :destination, :class_name=>"Destination"
belongs_to :device
accepts_nested_attributes_for :origin, :destination, :waypoints
end
class Device < ActiveRecord::Base
attr_accessible :id, :name, :password
attr_accessible :device_driver_bind_attributes, :drivers_attributes, :routes_attributes, :current_location_attributes
has_many :drivers, through: :device_driver_bind
has_many :device_driver_bind, dependent: :destroy
has_one :current_location, :class_name => "CurrentLocation"
has_many :routes
has_many :origins, through: :routes
has_many :destinations, through: :routes
has_many :waypoints, through: :routes
accepts_nested_attributes_for :routes, :current_location, :device_driver_bind
end
<%= f.select(:id, options_for_select( Device.all.collect{|d| [d.name + "/" + d.get_driver().name, d.id] } ),:prompt=>"select a device") %>