保存记录在不显示任何错误消息的情况下不起作用-Rails

时间:2018-07-01 20:18:25

标签: ruby ruby-on-rails-4

我有一个Entreprise模型,该模型属于user_id列所标识的所有者。

当我尝试创建空白企业时,没有看到错误消息。所以我不知道如何解决它。需要查看错误消息,例如“名称为空”等。

这是我的控制器代码:

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

我的企业模式:

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 %>

感谢帮助。

1 个答案:

答案 0 :(得分:1)

您认为新的企业

<% if @entreprise.errors.any? %>
    <% @entreprise.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  <% end %>