0
,我發現了以下錯誤...的ActiveRecord :: AssociationTypeMismatch設備(#XXXX),得到的字符串(#XXXX)預期的ActiveRecord :: AssociationTypeMismatch設備(#XXXX),得到了預期的字符串(#XXXX)
我看不出來這個錯誤來自哪裏或如何解決它。任何人都可以解釋我的代碼出錯了嗎?
模型=
class Device < ActiveRecord::Base
has_many :from_links, class_name: "Link", foreign_key: "from_device"
has_many :to_links, class_name: "Link", foreign_key: "to_device"
def connected_devices
from_links.map(&:to_device) + to_links.map(&:from_device)
end
end
class Link < ActiveRecord::Base
belongs_to :from_device, class_name: "Device"
belongs_to :to_device, class_name: "Device"
end
LinksController =
class LinksController < ApplicationController
def create
@link = Link.new(link_params)
@device = Device.find_by(en: params[:link][:from_device])
if @link.save
flash[:success] = "Link created."
redirect_to device_path(@device)
else
render 'new'
end
end
private
def link_params
params.require(:link).permit(:from_device, :to_device, device_attributes: [:en])
end
end
DevicesController =
class DevicesController < ApplicationController
def show
@device = Device.find(params[:id])
@linked_devices = @device.connected_devices
end
private
def device_params
params.require(:device).permit(:name, :en, :system_ip, :router_ip, :pop, :chassis_mode, :code_version)
end
CreateLinks數據庫=
class CreateLinks < ActiveRecord::Migration[5.0]
def change
create_table :links do |t|
t.references :from_device
t.references :to_device
end
end
創建新鏈接形式=
<% provide(:title, 'New Link') %>
<h1> Create New Link </h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@link, url: links_path(params[:id])) do |f| %>
<%= render 'shared/error_messages', object: @link %>
<%= f.label :from %>
<%= f.select :from_device, options_for_select(@devices), :include_blank => true, class: 'form-control' %>
<%= f.label :to %>
<%= f.select :to_device, options_for_select(@devices), :include_blank => true, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
什麼是給出這個錯誤?我認爲它告訴你,無論varibable拋出的錯誤是一個字符串,它期望一個設備ID。 – gwalshington
它說,這條線是錯誤的地方....提取源(繞線#8):def創建@link = Link.new(link_params)。點擊提交按鈕保存「創建新鏈接」即可。 –
你也可以在問題中發佈你的參數嗎? – gwalshington