Rails controller use other db for destroy method (update, create not working too, only show)

痴心易碎 提交于 2019-12-13 04:37:49

问题


i have such model:

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

(some db is converted from big catalog, so rails conventions are a little bit missed)

my show view of to_type is:

part of it:

 %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"

my show action work normally, also controller:

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

and route:

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

  end

but when i try to call destroy method i get:

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

also when i try edit or create i get:

undefined method `model_name' for NilClass:Class

i think that something is bad with connection with model: with update and create it didn't initialize object. With destroy it use other! db. What happens?

Also i try to recreate it all and rename, nothing... Could understand what wrong... Also when in model i write which db table to use same errors appear.

when i try to add new object via console all is ok.

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:


you don't have relationship between ToArticle and ToType in database. use belongs_to in ToArticle migration

check rails guide on associations



来源:https://stackoverflow.com/questions/18215087/rails-controller-use-other-db-for-destroy-method-update-create-not-working-too

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!