Ruby on Rails Friday, August 16, 2013



On Wednesday, 14 August 2013 07:53:20 UTC-4, Павел Добренко wrote:
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

This is likely the problem - ActiveModel expects an instance of ToType to have a method to_model. The default implementation just returns the object, but the belongs_to here is overriding that and messing things up.

I'd recommend naming the association something else.

--Matt Jones

 
      has_many :to_articles, :foreign_key => "to_type_id", :dependent => :destroy
    end
    
    class ToArticle < ActiveRecord::Base
      attr_accessible :details, :manufacturer, :name, :oem_number, :only_with_vin, :quantity, :to_type_id
      belongs_to :to_type, :foreign_key => "TYP_ID"
      belongs_to :to_engine
    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

form partial:

    = form_for [:admin, :catalog, :to, @typ] do |f|
      = f.label :id
    ...

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. In log i see that object is founded in db for edit for example, and in log i can see that @man is not empty, but i still get undefined method `model_name' for NilClass:Class . Why?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f9c7bc2e-3004-4665-a727-37d366536f29%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment