【问题标题】:unknown attribute X, but X is not in argument of update_attributes in rails3未知属性 X,但 X 不在 rails3 中 update_attributes 的参数中
【发布时间】:2012-06-13 05:10:55
【问题描述】:

我正在尝试使用 accept_nested_attributes 在表单中设置对象的字段。但是当我这样做时在控制器中:

@device.update_attributes(params[:device])

我明白了:

ActiveRecord::UnknownAttributeError
"unknown attribute: device_id" 

但是 device_id 是其他非相关模型的属性,不包含在 params 中。 参数如下。

{"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"}

什么可以被认为是原因。 这是我的代码。

查看

<%= form_for @device, :url => {:action => "do_compose"}, :method => :post do |f| %>
  <div class="field">
     <%= select_tag(:id, options_for_select( Device.all.collect{|d| [d.name + "/" + d.get_driver().name, d.id] } ),:prompt=>"select a device") %>                                 
  </div>

  <div class="field">
    <%= render partial:"routes/nested_routes_form", locals: {route_object:@device.get_route(), parent_form:f} %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

控制器

  def do_compose
    @device = Device.find(params[:id])
    respond_to do |format|
      if @device.update_attributes(params[:device])
        format.html { redirect_to @device, notice: 'Device was successfully updated.' }
      else
        format.html { render action: comopse }
      end
    end
  end

型号

  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 

【问题讨论】:

  • 您可能需要检查 Route 是否缺少 :device_id 列。 AR 期望能够用accepts_nested_attributes_for :routes 设置这个,以满足belongs_to :device
  • 就是这样!这么简单的错误..非常感谢!!!
  • 我会把它变成一个完整的答案,然后你就可以将它标记为已解决。

标签: ruby-on-rails-3 update-attributes


【解决方案1】:

这一定是您的 select_tag 中的问题,试试这个:

<%= f.select(:id, options_for_select( Device.all.collect{|d| [d.name + "/" + d.get_driver().name, d.id] } ),:prompt=>"select a device") %>  

【讨论】:

  • 确切地说是 f.select :=)
  • 感谢您的回复,但没有奏效。我在 do_compose 操作中同时更改为 @device = Device.find(params[:device][:id]) ,并得到相同的错误..
  • 顺便说一句,在做“@device = Device.find(params[:id])”时Device对象的一些属性已经设置好了。我正在尝试通过@device.update_attributes(params[:device]) 设置其余属性。
【解决方案2】:
ActiveRecord::UnknownAttributeError
"unknown attribute: device_id"

看起来是因为 Routes 上缺少一列。

ActiveRecord 期望能够使用accepts_nested_attributes_for :routes 进行设置,以满足belongs_to :device

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多