Nameerror (uninitialized Constant Sistusuario::Empresasistusuario):

Asked

Viewed 1,180 times

1

Hello, I did a CRUD with scaffold, and now I’m trying to add something else in the methods, I’m beginner in ruby on Rails,when entering Edit mode,presents me the following error and I can not understand where it is not initialized...

NameError (uninitialized constant SistUsuario::EmpresaSistUsuario):
  app/models/sist_usuario.rb:16:in `get_empresa_type_id'
  app/controllers/sist_usuarios_controller.rb:54:in `edit'
  app/controllers/application_controller.rb:115:in `do_with_current_user'

Below is my controller,model and view(form,which is loaded into the Edit view)

Controller

# encoding: utf-8
class SistUsuariosController < ApplicationController
  before_filter :require_user


  add_breadcrumb 'Administração > Sistema > Usuários ', nil

  def index
  end

  def grid_data
    select = "sist_usuarios.id, 
              sist_usuarios.susu_nome, 
              empresas.emp_fantasia, 
              sist_perfils.perf_desc, 
              sist_usuarios.susu_cpf, 
              sist_usuarios.susu_status"
    joins = ""
    conditions = ""
    group = ""

    joins = "INNER JOIN sist_perfils ON sist_perfils.id = sist_usuarios.sist_perfils_id
             INNER JOIN empresas ON empresas.id = sist_usuarios.susu_origem_id"
    conditions = ""
    conditions_search = ""
    if params[:sSearch] != nil && params[:sSearch].length > 0
      conditions_search += "(sist_usuarios.susu_nome like '%#{params[:sSearch]}%' 
                             OR sist_usuarios.susu_cpf like '%#{params[:sSearch]}%' 
                             OR empresas.emp_fantasia like '%#{params[:sSearch]}%' 
                             OR sist_perfils.perf_desc like '%#{params[:sSearch]}%')" 
    end
    @result = create_grid_json SistUsuario, select, conditions, conditions_search, "sist_usuarios.susu_nome", joins, group
    render layout: false, inline: @result
  end

  def show
    @sist_usuario = SistUsuario.find(params[:id])
    #respond_with(@sist_usuario)
    # @sist_usuario = SistUsuario.find(params[:id])
  end

  def new
    @sist_usuario = SistUsuario.new
  end

  def create
    @sist_usuario = SistUsuario.new(params[:sist_usuario])
    @sist_usuario.save
    respond_with(@sist_usuario)
  end

  def edit
    @sist_usuario = SistUsuario.find(params[:id])
    @empresa_origem_options = SistUsuario.get_empresa_type_id params[:id]
    #@enterprise_type_construcao_reconstrucao = Enterprise.get_enterprise_type_id_by_module params[:id],SystemModule::CONSTRUCAO_RECONSTRUCAO
    #@enterprise_type_conservacao = Enterprise.get_enterprise_type_id_by_module params[:id],SystemModule::CONSERVACAO
    #@enterprise_type_lvc = Enterprise.get_enterprise_type_id_by_module params[:id],SystemModule::LVC

  end

  def update
    #@sist_usuario = SistUsuario.new(params[:sist_usuario])

    respond_with(@sist_usuario)
    current_rels = EnterpriseTypeEnterprise.where("emp_fantasia = ?", @empresa.id)

    if @sist_usuario.update_attributes(params[:sist_usuario])

      unless current_rels.blank?
        current_rels.each do |r|
          r.mark_as_deleted
        end
      end

    end  
  end

  def destroy
    if @sist_usuario.destroy
      redirect_to sist_usuarios_url, :notice => "Exclusão realizada com sucesso."
    else
      flash[:error] = "Este registro já possui outros registros relacionados."
      render :action => 'index'
    end
  end

  #def edit
    #  @sist_usuario = id.find_by(params[:sist_usuario])
    #  respond_with(@sist_usuario)
  #end  
end

Model

#encoding: utf-8
class SistUsuario < ActiveRecord::Base
  belongs_to :empresa
  belongs_to :sist_perfil

  attr_accessible  :id, :susu_origem_id, :susu_nome, :susu_cpf, :sist_perfils_id, :susu_criptopassw, :susu_status, :emp_fantasia, :perf_desc

  # validates_presence_of :susu_origem_id, :message => "O campo Empresa de Origem não pode ser vazio."
  # validates_presence_of :susu_cpf, :message => "O campo CPF do usuráio não pode ser vazio."
  # validates_presence_of :susu_nome, :message => "O campo Nome do usuário não pode ser vazio."
  # validates_presence_of :sist_perfils_id, :message => "O campo Perfil do usuário não pode ser vazio."

  # validates_uniqueness_of :susu_cpf, :message => "Este CPF já está cadastradO. Verifique!!!"
  def self.get_empresa_type_id emp_fantasia
    #types = empresa.find(emp_fantasia).empresa
    types = EmpresaSistUsuario.all :joins => "INNER JOIN empresas ON empresas.emp_fantasia = situ_usuario_empresa.empresa_type_id",
                                         :conditions =>["situ_usuario_empresa.empresa_type_id = ?", emp_fantasia]

     result = nil           
     result = situ_usuario_empresa

      return result
    #end

  end  




end

Form

<%= render :partial => 'shared/shortcuts', :locals => {
        :type => SistUsuario,
        :show_new => false,
        :show_edit => false,
        :show_destroy => false,
        :additional_methods => { }
} %>

  <h2>Dados do Usuário</h2>



<%= form_for @sist_usuario do |f| %>
  <%= render "shared/form_errors", :target => @sist_usuario %>
    <%= field_set_tag do %>
      <%
        @empresa_origem_options = {:include_blank => "Selecione a empresa"}
        unless @empresa_origem.blank?
        @empresa_origem_options[:selected]= @empresa_origem
        end
        @perfil_origem_options = {:include_blank => "Selecione o perfil"}
        unless @perfil_origem.blank?
        @perfil_origem_options[:selected]= @perfil_origem
        end
      %>
      <p>
        <%= f.label :susu_origem_id, "Empresa de Origem", :class => "asterisk" %><br />
        <%= f.select :emp_fantasia, empresa.where("id = ?").collect {|p| [ p.emp_fantasia, p.id ] }, @empresa_origem_options %>
      </p>

      <p>
        <%= f.label :susu_nome, "Nome do usuário", :class => "asterisk" %><br />
        <%= f.text_field :susu_nome %>
      </p>

      <p>
        <%= f.label :susu_cpf, "Cpf do usuário", :class => "asterisk" %><br />
        <%= f.text_field :susu_cpf %>
      </p>

      <p>
        <%= f.label :susu_status, "Status" %><br />
        <%= f.text_field :susu_status %>
      </p>
    <% end %>

       <p>
        <%= f.label :sist_perfils_id, "Perfil do usuário", :class => "asterisk" %><br />
        <%= f.select :perf_desc, sist_perfil.where("id = ?").collect {|p| [ p.perf_desc, p.id ] }, @perfil_origem_options %>
      </p>
    <p>
      <%= render "shared/buttons_form", :f => f, :target => @sist_usuario, :return_to_path => "sist_usuarios" %>
    </p>
<%end%>



I apologize if I was not totally clear, I am available for any doubts... I thank you in advance

2 answers

0

Already tried adding :: before model name?

Example:

  types = ::EmpresaSistUsuario.all :joins => # etc...

0

The Rails is not finding Empresasistusuario, is this a model ever created by you? Check on ./app/models/empresa_sist_usuario.Rb

Browser other questions tagged

You are not signed in. Login or sign up in order to post.