I'm having this problem if the validator encounters any errors after submitting the form. I think that may be why my instance variable, @users, is nil?
I wrap it in the controller with the before_action, so should not have this problem, correct?
Controller:
class EnterprisesController < ApplicationController
before_action :filter_user, only: [:new, :edit]
def new
@enterprise = Enterprise.new
respond_with(@enterprise)
end
def edit
end
private
def filter_user
@users = User.where("role = 'customer' AND ID NOT IN ( SELECT user_id FROM ENTERPRISES WHERE USER_ID IS NOT NULL)")
end
end
View:
<%= simple_form_for(@enterprise) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :name, label: 'Nome' %>
<%= f.input :corporate_name, label: 'Razão Social' %>
<%= f.input :phone, label: 'Telefone' %>
<%= f.input :cnpj, label: 'CNPJ' %>
<%= f.input :state_registration, label: 'Inscrição Estadual' %>
<%= f.input :adress, label: 'Endereço' %>
<%= f.input :number, label: 'Número' %>
<%= f.input :district, label: 'Bairro' %>
<%= f.input :city, label: 'Cidade' %>
<%= f.input :cep, label: 'CEP' %>
<%= f.label :user_id, 'Usuário' %>
<%= f.collection_select(:user_id, @users, :id, :email, {include_blank: true}) %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
I searched the internet for what it could be, and found a solution; consult the bank directly from the view. But this violates the MVC principles, so I would have to consult on the Controller myself, but I can not solve the problem.