2014-09-12 86 views
0

我想在我的項目來驗證第二個模型,但不顯示我的錯誤,驗證第二個模型在Ruby on Rails的

項目屬於Compras,當一個項目到compras添加,並作出錯誤,不告訴我的消息的錯誤

本所認爲

<%- model_class = Compra -%> 
<div class="page-header"> 
    <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1> 
</div> 

<dl class="dl-horizontal"> 
    <dt><strong><%= model_class.human_attribute_name(:numBoleta) %>:</strong></dt> 
    <dd><%= @compra.numBoleta %></dd> 
    <dt><strong><%= model_class.human_attribute_name(:monto) %>:</strong></dt> 
    <dd><%= @compra.monto %></dd> 
    <dt><strong><%= model_class.human_attribute_name(:comentarios) %>:</strong></dt> 
    <dd><%= @compra.comentarios %></dd> 
    <dt><strong><%= model_class.human_attribute_name(:fechaHora) %>:</strong></dt> 
    <dd><%= @compra.fechaHora %></dd> 
</dl> 
<dl class="dl-horizontal"> 
<h2>Agregar Item </h2> 
<h3>Errores</h3> 
<% if @item.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@item.errors.count, "error") %> prohibited 
     this item from being saved:</h2> 
    <ul> 
    <% @item.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
    <% end %> 





<h2>Items de la compra </h2> 
<% @compra.items.each do |item| %> 
<p> 
    <strong>Nombre:</strong> 
    <%= item.nombre %> 
    </p> 

    <p> 
    <strong>Cantidad:</strong> 
    <%= item.cantidad %> 
    </p> 
    <p> 
    <strong>Valor:</strong> 
    <%= item.valor %> 
    </p> 
<% end %> 




<%= form_for([@compra, @compra.items.build]) do |f| %> 
    <p> 
    <%= f.label :nombre %><br> 
    <%= f.text_field :nombre %> 
    </p> 
    <p> 
    <%= f.label :cantidad %><br> 
    <%= f.text_field :cantidad %> 
    </p> 
    <p> 
    <%= f.label :valor %><br> 
    <%= f.text_field :valor %> 
    </p> 

    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

這是控制器

class ComprasController < ApplicationController 

    def new 
     @compra = Compra.new 

    end 
    def create 
     @compra = Compra.new(compra_params) 

     if @compra.save 
     redirect_to @compra 
     else 
     render 'new' 
     end 
    end 
    def show 
     @compra = Compra.find(params[:id]) 
     @item = Item.new 
    end 
    def index 
     @compras = Compra.all 

    end 


private 
    def compra_params 
    params.require(:compra).permit(:numBoleta, :monto, :comentarios,:fechaHora) 
    end 
end 

這是模型

class Item < ActiveRecord::Base 
    belongs_to :compra 
    validates_format_of :cantidad, :with => /[0-9]/,:message => "must contain at least one number" 
    validates :valor, presence: true 
end 

謝謝。

+0

不知道這僅僅是英語不是你的第一語言的問題,但你一直說不會顯示錯誤消息。你的意思是它不顯示?你的問題有點不清楚。請注意,我不是試圖貶低只是試圖瞭解問題所在。 – engineersmnky 2014-09-12 19:34:06

回答