嵌套表单不验证关联模型

时间:2012-09-17 14:19:33

标签: ruby-on-rails ruby-on-rails-3 activerecord

我正在使用带有活动记录和sqlserver的rails 3.2。我有嵌套表单的问题。我有一个模型注册,其中有许多注册细节,每个都有一个人关联。

这是模型

class Registration < ActiveRecord::Base
    set_table_name "dbo.EV_INSCRIPCIONES"
    set_primary_key "Id"

    belongs_to :category, :foreign_key => 'CategoriaId'
    has_many :registrationDetails, :foreign_key => 'InscripcionEventoId'
    #has_one :group

    accepts_nested_attributes_for :registrationDetails
    validates_associated :registrationDetails

    validates :Refencia, :presence => true

    #validates_presence_of :category_id

    attr_accessible :registrationDetails, :category,  :Eliminada, :FechaInscripcion, :CreationalDate, :Referencia, :PagoRegistrado, :Acreditado, :registrationDetails_attributes

    #after_initialize :init 

    def init
        write_attribute :Eliminada, false
        write_attribute :Acreditado, false
        write_attribute :PagoRegistrado, false
        write_attribute :CreationalDate, DateTime.now
        write_attribute :FechaInscripcion, DateTime.now
        #write_attribute :Referencia, ''
    end

    def category_id
        self.category.id unless category.nil?
    end

    def category_id=(id)
        self.category = Category.find(id)
    end


end

class RegistrationDetail < ActiveRecord::Base
    set_table_name "dbo.EV_INSCRIPCION_DETALLE"
    set_primary_key "Id"

    belongs_to :registration, :foreign_key => 'InscripcionEventoId'
    belongs_to :category, :foreign_key => 'CategoriaId'
    belongs_to :person, :foreign_key => 'ParticipanteId', :primary_key => 'JUGADOR_ID'

    attr_accessible :person, :category, :registration, :Eliminada, :person_attributes

    accepts_nested_attributes_for :person
    validates_associated :person

    after_initialize :init 


    def init
        write_attribute :Eliminada, false

    end

end

class Person < ActiveRecord::Base
    set_table_name "dbo.PKR_JUGADOR"
    set_primary_key "JUGADOR_ID"

    has_many :registrationDetails

    validates_presence_of :CDNI, :CNOMBRES, :CAPELLIDO, :CSEXO,:CCIUDADRESIDENCIA, :DFECHANACIMIENTO
    validates :CDNI, :length => { :minimum => 7, :maximum =>8 }
    #validate :validate_birth_date

    before_save :set_ids

    def set_ids 
        if id.nil?
            _id = Person.maximum(:JUGADOR_ID)
            self.JUGADOR_ID = _id +1
            write_attribute :CNROJUGADOR, _id+1         
        end 
    end     

    after_initialize :init
    def init        
        write_attribute :TIPODOCUMENTO_ID, 1 if read_attribute(:TIPODOCUMENTO_ID).nil? 
    end

    def full_name
        self.surname + ", " + self.name
    end

    protected
        def validate_birth_date
            errors.add(:birth_date, 'must be a valid datetime') if ((Date.strptime(birth_date, "%d/%m/%Y") rescue ArgumentError) == ArgumentError)
        end

我的观点

index.html.erb

<h1><%= t '.title', :name => @event.CNOMBRETORNEO %> </h1>
<%= render 'form_nested' %>

_form_nested

<%= form_for @registration, :url => {:controller => "registration", :action => "save"}, :html => {:class=> 'form-horizontal'} do |f| %>
    <legend><%= t '.legend' %></legend> 
    <% if f.object.errors.any?%>
    <div id="error_explanation">
        <h3 class="text-error"><%= t '.has-errors' %></h3>
    </div>
    <% end %>


    <input type="hidden" id="event_date" name="event_date" value="<%= @event.DDIAACTIVIDAD.strftime('%Y%m%d') %>" />
    <input type="hidden" id="event_id" name="event_id" value="<%= @event.TORNEOPOKER_ID %>" />

    <%= f.fields_for :registrationDetails do |d|  %>
        <%= render 'details_fields' , :f => d %>
    <% end %>

    <% has_error = f.object.errors.has_key? :category %>
    <div class="control-group<%= " error" if has_error %>">
        <%= f.label :category, :class=> 'control-label' %>
        <div class="controls">
            <%= f.hidden_field :category_id %>
            <div class="btn-group" data-toggle-name="presenter_category_id" data-toggle="buttons-radio">
            <%@categorias.each do |x| %>
                <button value="<%= x.Id %>" type="button" class="btn" data-age-from="<%= x.FromAge %>" data-age-to="<%= x.ToAge %>"><%= x.Name %></button>
            <%end  %>
            </div>
            <% if has_error  %>         
            <span class="help-inline"><%=f.object.errors[:category].join(", ")%></span>
            <% end %>
        </div>
    </div>

    <div class="form-actions">
        <button type="submit" class="btn btn-primary"><%= t '.save' %></button>
    </div>

<%end%>

_detatails_fields

<%= f.object.errors.inspect %>

<%= f.fields_for :person do |p| %>
    <%= render 'person_fields', :f => p %>
<% end %>

_person_fields

<%= f.hidden_field :id %>
<% has_error = f.object.errors.has_key? :CDNI %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :CDNI, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-book"></i></span>
            <%= f.text_field :CDNI, :class => 'input' %>
            <div class="add-on" id="document_loader"><%= image_tag '6-0.gif' %></div>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:CDNI].join(", ")%></span>
        <% end %>
    </div>
</div>

<% has_error = f.object.errors.has_key? :DFECHANACIMIENTO %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :DFECHANACIMIENTO, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-calendar"></i></span>
            <%= f.text_field :DFECHANACIMIENTO, :class=>'input' %>
            <span id="person_age" class="add-on"></span>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:DFECHANACIMIENTO].join(", ")%></span>
        <% end %>
    </div>
</div>

<% has_error = f.object.errors.has_key? :CNOMBRES %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :CNOMBRES, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-user"></i></span>
            <%= f.text_field :CNOMBRES,:class=>'input' %>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:CNOMBRES].join(", ")%></span>
        <% end %>
    </div>
</div>
<% has_error = f.object.errors.has_key? :CAPELLIDO %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :CAPELLIDO, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-user"></i></span>
            <%= f.text_field :CAPELLIDO,:class=>'input' %>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:CAPELLIDO].join(", ")%></span>
        <% end %>
    </div>
</div>

<div class="control-group">
    <%= f.label :CTELEFONO, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-certificate"></i></span>
            <%= f.text_field :CTELEFONO,:class=>'input' %>
        </div>
    </div>
</div>
<% has_error = f.object.errors.has_key? :CSEXO %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :CSEXO, :class=> 'control-label' %>
    <div class="controls">
        <%= f.hidden_field :CSEXO %>
        <div class="btn-group" data-toggle-name="presenter_sex" data-toggle="buttons-radio">
            <button type="button" class="btn" value="M"><%= t '.masculine' %></button>
            <button type="button" class="btn" value="F"><%= t '.femenine' %></button>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:CSEXO].join(", ")%></span>
        <% end %>
    </div>
</div>      
<% has_error = f.object.errors.has_key? :CCIUDADRESIDENCIA %>
<div class="control-group<%= " error" if has_error %>">
    <%= f.label :CCIUDADRESIDENCIA, :class=> 'control-label' %>
    <div class="controls">
        <div class="input-append">
            <span class="add-on"><i class="icon-map-marker"></i></span>
            <%= f.text_field :CCIUDADRESIDENCIA,:class=>'input' %>
        </div>
        <% if has_error  %>         
        <span class="help-inline"><%=f.object.errors[:CCIUDADRESIDENCIA].join(", ")%></span>
        <% end %>
    </div>
</div>

最后是控制器

class RegistrationController < ApplicationController
    def index
        date = "20120729"
        id = 1

        #id = params[:event_id] unless params[:event_id].nil?
        #date = params[:event_date] unless params[:event_date].nil?

        @event = Event.find(date,id)
        @groups = Group.all
        @categorias = Category.where("DiaActividad = ? and TorneoId = ?", date, id)


        @registration = Registration.new
        @registration.registrationDetails.build( :person => Person.new)
    end


    def save
        @registration = Registration.new(params[:registration])
        if @registration.save
            redirect_to :controller => 'home'
        else
            date = params[:event_date]
            id = params[:event_id]  
            @event = Event.find(date,id)
            @groups = Group.all
            @categorias = Category.where("DiaActividad = ? and TorneoId = ?", date, id)
            render :action => 'index'
        end
    end

end

发生了什么事?首先,不保存任何字段的更改,并且不显示验证。当我提交表单时,它返回到同一页面,似乎表单有错误但没有人显示,数据丢失。

希望你能帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

嗯,我不知道为什么,但我解决了使用资源的问题。

在我的routes.rb文件

resources :registrations

在我有这样的事情之前。

  match 'registration/(:event_date)/(:event_id)' => 'registration#index'
  match 'registration/save' => 'registration#save'

我认为问题是我的自定义路线。但我真的不知道。