Rails控制器使用其他db for destroy方法(更新,创建不工作,只显示)

时间:2013-08-13 17:03:40

标签: ruby-on-rails ruby activerecord

我有这样的模特:

class ToType < ActiveRecord::Base
  attr_accessible :Name, :TYP_CCM, :TYP_CCM_TAX, :TYP_CDS_ID, :TYP_CTM, :TYP_CYLINDERS, :TYP_DOORS, :TYP_HP_FROM, :TYP_HP_UPTO, :TYP_ID, :TYP_KV_ABS_DES_ID, :TYP_KV_ASR_DES_ID, :TYP_KV_AXLE_DES_ID, :TYP_KV_BODY_DES_ID, :TYP_KV_BRAKE_SYST_DES_ID, :TYP_KV_BRAKE_TYPE_DES_ID, :TYP_KV_CATALYST_DES_ID, :TYP_KV_DRIVE_DES_ID, :TYP_KV_ENGINE_DES_ID, :TYP_KV_FUEL_DES_ID, :TYP_KV_FUEL_SUPPLY_DES_ID, :TYP_KV_MODEL_DES_ID, :TYP_KV_STEERING_DES_ID, :TYP_KV_STEERING_SIDE_DES_ID, :TYP_KV_TRANS_DES_ID, :TYP_KV_VOLTAGE_DES_ID, :TYP_KW_FROM, :TYP_KW_UPTO, :TYP_LA_CTM, :TYP_LITRES, :TYP_MAX_WEIGHT, :TYP_MMT_CDS_ID, :TYP_MOD_ID, :TYP_PCON_END, :TYP_PCON_START, :TYP_RT_EXISTS, :TYP_SORT, :TYP_TANK, :TYP_VALVES, :is_in_to
  set_primary_key :TYP_ID
  belongs_to :to_model
  has_many :to_articles, :dependent => :destroy
end

class ToArticle < ActiveRecord::Base
  attr_accessible :details, :manufacturer, :name, :oem_number, :only_with_vin, :quantity, :type_id
  belongs_to :to_type
end

(某些数据库是从大目录转换而来的,因此有点遗漏了rails惯例)

我对to_type的显示视图是:

部分内容:

 %td
            = link_to "Подробнее", admin_catalog_to_to_article_path(c), :class=>'btn btn-primary'
            = link_to "Редактирование", edit_admin_catalog_to_to_type_path(c), :class=>'btn btn-warning'
            = link_to "Удалить", admin_catalog_to_to_type_path(c), :confirm => "!!!Тип #{c.Name} будет удалён!!!! Вы уверены?", :method => :delete, :class => "btn btn-danger"

我的节目动作正常,也是控制器:

class Admin::Catalog::To::ToTypesController < ApplicationController
  respond_to :html

  before_filter :auth_user

  def auth_user
    redirect_to new_admin_session_path unless admin_signed_in?
  end

  def show
    @mod_id = params[:id]
    @man = ToType.find(:all, conditions: {:TYP_MOD_ID => @mod_id}, order: "Name ASC")
    render :layout => 'admin'
  end

  def edit
    @man = ToType.find(params[:id])
    render :layout => 'admin'
  end

  def update
    @man = ToType.find(params[:id])
    if @man.update_attributes(params[:to_type])
      redirect_to admin_catalog_to_to_type_path(@man.TYP_MOD_ID)
    else
      render :layout => 'admin'
    end
  end

  def new
    @man = ToType.new
    @mod_id = params[:mod_id]
    render :layout => 'admin'
  end

  def create
    @man = ToType.new(params[:to_type])
    @mod_id = params[:mod_id]
    @man.TYP_MOD_ID = @mod_id
    if @man.save
      redirect_to admin_catalog_to_to_type_path(@mod_id)
    else
      render :layout => 'admin'
    end
  end

  def destroy
    @man = ToType.find(params[:id])
    if @man.destroy
      redirect_to admin_catalog_to_to_type_path(@man.TYP_MOD_ID)
    else
      render :layout => 'admin'
    end
  end
end

和路线:

namespace :admin do
  namespace :catalog do
      namespace :to do
        resources :to_manufacturers, 
                  :to_models, 
                  :to_types, 
                  :to_articles
      end
    end

  end

但是当我尝试调用destroy方法时,我得到:

ActiveRecord::StatementInvalid in Admin::Catalog::To::ToTypesController#destroy

Mysql2::Error: Unknown column 'to_articles.to_type_id' in 'where clause': SELECT `to_articles`.* FROM `to_articles`  WHERE `to_articles`.`to_type_id` = 26923

当我尝试编辑或创建我得到:

undefined method `model_name' for NilClass:Class

我认为与模型连接有些不好:使用update并创建它没有初始化对象。 用破坏它使用其他! D b。会发生什么?

此外,我尝试重新创建它并重命名,没有...可以理解错误...同样在模型中我写出哪个数据库表使用相同的错误。

当我尝试通过控制台添加新对象时,一切正常。

UPD:

class CreateToTypes < ActiveRecord::Migration
  def change
    create_table :to_types, :primary_key => :TYP_ID do |t|
      t.integer :TYP_ID
      t.integer :TYP_CDS_ID
      t.integer :TYP_MMT_CDS_ID
      t.integer :TYP_MOD_ID
      t.binary :TYP_CTM
      t.binary :TYP_LA_CTM
      t.integer :TYP_SORT
      t.integer :TYP_PCON_START
      t.integer :TYP_PCON_END
      t.integer :TYP_KW_FROM
      t.integer :TYP_KW_UPTO
      t.integer :TYP_HP_FROM
      t.integer :TYP_HP_UPTO
      t.integer :TYP_CCM
      t.integer :TYP_CYLINDERS
      t.integer :TYP_DOORS
      t.integer :TYP_TANK
      t.integer :TYP_KV_VOLTAGE_DES_ID
      t.integer :TYP_KV_ABS_DES_ID
      t.integer :TYP_KV_ASR_DES_ID
      t.integer :TYP_KV_ENGINE_DES_ID
      t.integer :TYP_KV_BRAKE_TYPE_DES_ID
      t.integer :TYP_KV_BRAKE_SYST_DES_ID
      t.integer :TYP_KV_FUEL_DES_ID
      t.integer :TYP_KV_CATALYST_DES_ID
      t.integer :TYP_KV_BODY_DES_ID
      t.integer :TYP_KV_STEERING_DES_ID
      t.integer :TYP_KV_STEERING_SIDE_DES_ID
      t.float :TYP_MAX_WEIGHT
      t.integer :TYP_KV_MODEL_DES_ID
      t.integer :TYP_KV_AXLE_DES_ID
      t.integer :TYP_CCM_TAX
      t.float :TYP_LITRES
      t.integer :TYP_KV_DRIVE_DES_ID
      t.integer :TYP_KV_TRANS_DES_ID
      t.integer :TYP_KV_FUEL_SUPPLY_DES_ID
      t.integer :TYP_VALVES
      t.integer :TYP_RT_EXISTS
      t.string :Name
      t.boolean :is_in_to
      t.string :fuel_type
    end
  end
end


class CreateToArticles < ActiveRecord::Migration
  def change
    create_table :to_articles do |t|
      t.string :oem_number
      t.string :manufacturer
      t.text :name
      t.integer :quantity
      t.text :details
      t.boolean :only_with_vin
    end
  end
end

1 个答案:

答案 0 :(得分:1)

您在数据库中没有ToArticleToType之间的关系。 在belongs_to迁移

中使用ToArticle

检查关联的rails guide