У меня есть модель Entreprise
, в которой владелец указан в столбце user_id
.
Когда я пытаюсь создать пустое предприятие, я не вижу сообщений об ошибках. Так что я не знаю, как это исправить. Нужно видеть сообщения об ошибках типа "name is blank" и т.д ...
Это код моего контроллера:
class EntreprisesController < ApplicationController
before_action :authenticate_user! # pour acceder il faut etre identifier
authorize_resource # utilisation de cancancan pour les droits - voir models/ability.rb
def new
@entreprise = Entreprise.new
@entreprise.build_owner
end
def create
@entreprise = Entreprise.new(entreprise_params)
if @entreprise.save
redirect_to entreprise_path, notice: "creation faite"
else
render "entreprises/new"
end
end
private
def entreprise_params
params.require(:entreprise).permit(:subdomain, :name, :country, :town, :owner_id, :zipcode, :street, :state, :taxconfig )
end
end
Моя модель Entreprise:
class Entreprise < ApplicationRecord
RESTRICTED_SUBDOMAINS = %w(www)
belongs_to :owner, class_name: 'User', foreign_key: :user_id
validates :owner_id, presence: true
validates :name, presence: true, length: {maximum: 50}, uniqueness: { case_sensitive: false},
format: { with: /\A[\w\-]+\Z/i, message: 'contains invalid characters'}
validates :town, presence: true
validates :country, presence: true
validates :zipcode, presence: true
validates :subdomain,
uniqueness: { case_sensitive: false},
format: { with: /\A[\w\-]+\Z/i, allow_blank: true, message: 'contains invalid characters'},
exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'restricted'}
before_validation :downcase_subdomain
before_validation :downcase_name
has_many :users
private
def downcase_name
self.name = name.try(:downcase)
end
def downcase_subdomain
self.subdomain = subdomain.try(:downcase)
end
end
и модель пользователя:
class User < ApplicationRecord
ROLES = %i[user banned admin] # defini les roles possibles pour cancancan
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :fullname, presence: true, length: {maximum: 50}
validates :first_name, presence: true
validates :last_name, presence: true
validates :role, presence: true
has_attached_file :avatar, :style => { :medium => "300x300", :thumb => "100x100"},
:default_url => "/assets/default_image.jpeg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
has_many :account, inverse_of: :owner
end
и мой взгляд на новое предприятие:
<%= form_for @entreprise do |f| %>
<%= render 'shared/message' %>
<div class="row">
<div class="form-group">
<%= f.label :subdomain %>
<%= f.text_field :subdomain %>
</div>
</div>
<div class="row">
<div class="form-group">
<%= f.label :name, class: 'form-label' %><br />
<%= f.text_field :name, autofocus: true, autocomplete: "Nom",class: 'form-control' %>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label :street, class: 'form-label' %><br />
<%= f.text_field :street, autofocus: true, autocomplete: "Adress",class: 'form-control' %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.label :town, class: 'form-label' %><br />
<%= f.text_field :town, autofocus: true, autocomplete: "Ville",class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label :state, class: 'form-label' %><br />
<%= f.text_field :state, autofocus: true, autocomplete: "Etat",class: 'form-control' %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.label :country, class: 'form-label' %><br />
<%= f.text_field :country, autofocus: true, autocomplete: "Pays",class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label :zipcode, class: 'form-label' %><br />
<%= f.text_field :zipcode, autofocus: true, autocomplete: "Zipcode",class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="actions">
<%= f.submit t('entreprise.new.title'), class: "btn btn-normal btn-block" %>
</div>
</div>
</div>
<% end %>
Спасибо за помощь.