2011-08-17 24 views
0

我更新CLASS_NAME導軌3,未初始化常數Tercero :: terceroclasificacion

/models/tipotercero.rb

class Tipotercero < ActiveRecord::Base 
    has_many :terceroclasificaciones 
    has_many :terceros , :class_name => "Terceroclasificacion" 
end 

/models/tercero.rb

class Tercero < ActiveRecord::Base 
    has_many :ciudades 
    has_many :terceroclasificaciones 
    has_many :tipoterceros, :class_name => "Terceroclasificacion" 
end 


class Terceroclasificacion < ActiveRecord::Base 
    belongs_to :tercero 
    belongs_to :tipotercero 
    attr_accessor :tercero_id, :tipotercero_id 
end 

/視圖/ terceros/_form.html.erb

<div class="field"> 
    <% for tipotercero in Tipotercero.all %> 
    <div> 
     <%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, @tercero.tipoterceros.include?(tipotercero) %> 
     <%= tipotercero.nombre %> 
    </div> 
    <% end %> 
    </div> 

誤差

uninitialized constant Tercero::terceroclasificacion 

我曾試圖按照崗位 Rails 3 has_many :through Form,但在我的應用我找不到錯誤

添加terceros控制器

/controllers/terceros_controller.rb

class TercerosController < ApplicationController 
    # GET /terceros 
    # GET /terceros.xml 

    def index 
    @terceros = Tercero.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @terceros } 
    end 
    end 

    # GET /terceros/1 
    # GET /terceros/1.xml 
    def show 
    @tercero = Tercero.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @tercero } 
    end 
    end 

    # GET /terceros/new 
    # GET /terceros/new.xml 
    def new 
    @tercero = Tercero.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @tercero } 
    end 
    end 

    # GET /terceros/1/edit 
    def edit 
    @tercero = Tercero.find(params[:id]) 
    end 

    # POST /terceros 
    # POST /terceros.xml 
    def create 
    @tercero = Tercero.new(params[:tercero]) 

    # @tercero.attributes = {'tipotercero_ids' => []}.merge(params[:tercero] || {}) 

    respond_to do |format| 
     if @tercero.save 
     format.html { redirect_to(@tercero, :notice => 'Tercero was successfully created.') } 
     format.xml { render :xml => @tercero, :status => :created, :location => @tercero } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @tercero.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /terceros/1 
    # PUT /terceros/1.xml 
    def update 
    @tercero = Tercero.find(params[:id]) 

    respond_to do |format| 
     if @tercero.update_attributes(params[:tercero]) 

     format.html { redirect_to(@tercero, :notice => 'Tercero was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @tercero.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /terceros/1 
    # DELETE /terceros/1.xml 
    def destroy 
    @tercero = Tercero.find(params[:id]) 
    @tercero.destroy 

    respond_to do |format| 
     format.html { redirect_to(terceros_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

編輯3

頁面錯誤是:

NameError in Terceros#new 

Showing /home/andres/desarrollos/rubyonrails/proyecto/app/views/terceros/_form.html.erb where line #92 raised: 

uninitialized constant Tercero::terceroclasificacion 
Extracted source (around line #92): 

89: <div class="field"> 
90:  <% for tipotercero in Tipotercero.all %> 
91:  <div> 
92:  <%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, @tercero.tipoterceros.include?(tipotercero) %> 
93:  <%= tipotercero.nombre %> 
94:  </div> 
95:  <% end %> 
Trace of template inclusion: app/views/terceros/new.html.erb 

4編輯

錯誤創建tercero是:這

attr_accessor :tercero_id, :tipotercero_id 

我想你希望它是

NoMethodError in TercerosController#create 

undefined method `type_cast' for nil:NilClass 
Rails.root: /home/andres/desarrollos/rubyonrails/proyecto 

Application Trace | Framework Trace | Full Trace 
app/controllers/terceros_controller.rb:44:in `new' 
app/controllers/terceros_controller.rb:44:in `create' 
Request 

Parameters: 

{"tercero"=>{"identificacion"=>"1110465574", 
"empresa"=>"hogar", 
"tipo_identificacion"=>"2", 
"direccion1"=>"dierccion", 
"nombre"=>"carlos andres", 
"direccion2"=>"123", 
"ciudad_id"=>"2", 
"telefono_fijo"=>"132233", 
"telefono_movil"=>"123123", 
"fecha_nacimiento(1i)"=>"2013", 
"observaciones"=>"", 
"fecha_nacimiento(2i)"=>"10", 
"fecha_nacimiento(3i)"=>"17", 
"representante_legal"=>"", 
"tipotercero_ids"=>["1", 
"2", 
"3"], 
"apellido1"=>"colonia", 
"apellido2"=>"riveros", 
"pagina_web"=>""}, 
"commit"=>"Crear Tercero", 
"authenticity_token"=>"SeUoILctpNr9t6Lx8wSoHVTO5mjk0qJfnzJsb9Jtzao=", 
"utf8"=>"âœ「"} 
+0

錯誤不在您所顯示的視圖代碼中 – fl00r

+0

1.您什麼時候遇到錯誤?顯示完整的錯誤。 2.顯示視圖文件的行動將導致一個錯誤,不僅部分 – fl00r

回答

0

有時候,其他模型不能通過Rails約定從關聯名稱派生出來,並且會出現這種奇怪的錯誤。你可以通過這樣的類名:

has_many :terceroclasificaciones, :class_name => "Terceroclasificacion" 

而順便說一句,你可以讓你的生活更輕鬆,只需使用英文名稱爲你的模型和東西。反正你在視圖中進行本地化。

+0

你是對的..非常感謝,但是,現在讓我看看 # with checkbox any idea? – kalelc

+0

沒問題 - 你可以點擊勾號在我的答案,使其綠色;)至於你第二個問題嘗試在你的模型中定義to_s實例方法,返回名稱/標題/或任何你在那裏:) – socjopata

+0

藉口,我沒有承諾如何在模型中的實例方法:( – kalelc

0

嘗試刪除attr_accessible但不是attr_accessor

+0

已被刪除,但這是相同的。 :( – kalelc

相關問題