2012-09-17 31 views
0

我正在使用rails 3.2與活動記錄和sqlserver。我有嵌套窗體的問題。我有一個模型註冊,它有很多註冊信息,每個註冊人都有一個關聯的人。嵌套窗體不驗證關聯模型

這裏是模型

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 

這是怎麼回事?那麼首先,任何字段上的更改都不會保存,並且驗證不會顯示。當我提交表單時,它會返回到同一頁面,看起來表單有錯誤,但沒有人顯示,數據也會丟失。

希望你能幫助我。

在此先感謝

回答

0

嗯,我不知道爲什麼,但我解決了使用資源。

在我的routes.rb文件

resources :registrations 

之前,我有這樣的事情。

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

我想問題是我的自定義路線。但我真的不知道。