2015-02-10 3 views
1

я модель под названием RateIndex:Rails 4 - проверка не работает для модели

class RateIndex < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :car 

    validates :name,  :presence => true 
    validates :car_name, :presence => true 
    validates :description, :presence => true 
    ... 
end 

Контроллер rate_indices_controller.rb

class RateIndicesController < ApplicationController 
    def new 
    @rate_index = RateIndex.new 
    end 
    ... 

и форма:

<%= form_for(@rate_index) do |f| %> 
    <% if @rate_index.errors.any? %> 
    <div id="error_explanation"> 
     <h3><%= "#{pluralize(@rate_index.errors.count, "error")} prohibited this game from being saved:" %></h3> 
     <ul> 
     <% @rate_index.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    ... 

Но когда я отправляю пустую форму, ошибки проверки не отображаются ... Я попытался также изменить правило проверки с, но даже тогда они игнорируются.

Я отчасти ослеп, глядя в него довольно долго, но все равно ничего.

Буду Вам благодарен за любую помощь.

Спасибо

EDIT:

def create 
    @rate_index = RateIndex.new(rate_index_params) 
    respond_to do |format| 
     if @rate_index.save 
     format.html { redirect_to '/rate_indices/new', notice: 'Rate was successfully added.' } 
     format.json { render action: 'show', status: :created, location: @rate_index } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @rate_index.errors, status: :unprocessable_entity } 
     end 
    end 
end 
+1

Пожалуйста, добавьте 'ReateIndex # create' действия на ваш вопрос. – Rodrigo

+0

@Rodrigo - добавлен, спасибо. – user984621

+0

Является ли модель успешно сохраненной или она не отображает никаких сообщений? – BroiSatse

ответ

0

вам нужно вызвать valid? на вашем объекте первым, чтобы вызвать проверку

@rate_index.valid? 
@rate_index.errors 
+2

'valid?' Должен вызываться методом 'save', не так ли? – BroiSatse

Смежные вопросы