2016-11-28 1 views
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> 

+0

このエラーには何がありますか?私はそれがエラーを投げている変数が文字列であり、それがデバイスIDを期待していることを伝えていると思います。 – gwalshington

+0

これは、この行がエラーの場所であると言います。抽出されたソース(行#8):def create @link = Link.new(link_params)。 [Create New Link]を保存するには、[Submit]ボタンをクリックします。 –

+0

質問にあなたのパラメータを投稿することはできますか? – gwalshington

答えて

0

、あなたは列from_deviceto_deviceに関連を持っています。外部キーが何であれリンクテーブルを関連付ける必要があります。したがって、次のようなものがあります。

has_many :links, foreign_key: "from_device" 
has_many :links, foreign_key: "to_device" 

あなたのデバイスモデルと同じです。

関連する問題