2012-06-13 18 views
1

我正在嘗試使用accept_nested_attributes在窗體中設置對象的字段。然而,當我這樣做的控制器:未知屬性X,但X不在rails3中update_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 
+0

您可能需要檢查路由器是否缺少:device_id列。 AR期望能夠使用'acceptersted_attributes_for:routes'來設置它,以滿足'belongs_to:device' – CMW

+0

就是這樣!這樣一個容易的錯誤..非常感謝你! – Ryo

+0

我會把它變成一個完整的答案,然後你可以把它標記爲已解決。 – CMW

回答

0

這必須是在你的select_tag問題,試試這個:

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

這是f.select,確切地說:=) – blushrt

+0

謝謝你的迴應,但沒有奏效。在do_compose action中併發地改變了@device = Device.find(params [:device] [:id]),並且得到相同的錯誤。 – Ryo

+0

順便說一句,Device對象在執行「 @device = Device.find(params [:id])「已經設置。我試圖通過 @ device.update_attributes(params [:device])設置其餘的屬性。 – Ryo

0
ActiveRecord::UnknownAttributeError 
"unknown attribute: device_id" 

看起來像是由於路由上缺少一列。

ActiveRecord希望能夠將其設置爲accepts_nested_attributes_for :routes,以滿足belongs_to :device

相關問題