0

我需要一些幫助來正確處理Twitter Bootstrap的驗證。驗證工作正常,問題是Twitter Bootstrap Flash屏幕。這就是我的意思是:Rails 3.1+簡單嵌套表單驗證和Twitter引導控制狀態

任何字段填寫並提交了回報:http://i.stack.imgur.com/8pvUc.png

所需的4場的2提交的報稅表:http://i.stack.imgur.com/J6lCi.png (請注意,它不閃的錯誤)

我有一個籃球應用程序,玩家可以在許多球員(爲球員名冊歸檔目的)和陣容可以有很多球員。

Roster.rb

class Roster < ActiveRecord::Base 
    belongs_to :team 
    has_many :rosterizes 
    has_many :players, :through => :rosterizes 
    accepts_nested_attributes_for :players 
    validates_presence_of :jersey_number, :class_year 
    attr_accessible :jersey_number, :class_year, :players, :team_id, :players_attributes 
end 

Rosterizes.rb(名爲不好,我知道...)

class Rosterize < ActiveRecord::Base 
    belongs_to :player 
    belongs_to :roster 
    attr_accessible :player_id, :roster_id 
end 

Player.rb

class Player < ActiveRecord::Base 
    has_many :rosterizes 
    has_many :rosters, :through => :rosterizes 
    validates_presence_of :first_name, :last_name 
    attr_accessible :first_name, :last_name, :active 
end 

_add_player_form.html.erb (嵌套表格)

<%= simple_nested_form_for @roster, :url =>player_added_team_path, :html => { :class => 'form-horizontal' } do |f| %> 
    <%= f.simple_fields_for :players, @roster.players.build do |x| %> 
     <%= x.input :first_name %> 
     <%= x.input :last_name %> 
     <%= x.input :active, :as => :hidden, :input_html => {:value => true} %> 
    <%end%> 
    <%=f.input :class_year %>  
    <%=f.input :jersey_number %> 
    <%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%> 

    <div class="well"> 
    <%= f.button :submit, :class => 'btn-primary icon-plus-sign btn-success', :value => "Add To Team" %> 
    </div> 
<%end%> 

感謝您的幫助提前!

SOLUTION

我的問題是,每一個視圖加載時,它是建立一種新的形式,因此從來沒有找回錯誤的回滾

我做了一個改變我的控制器構建爲套裝形式

控制器

def add_player 
    ... 
    @new_player = @roster.players.build 
end 

並據此做出的更改視圖

_add_player_form.html.erb

<%= f.simple_fields_for :players, @new_player do |x| %> 
    ... 

<%end%> 

這並獲得成功,謝謝Mober!

回答

2

爲什麼只顯示第一級驗證的原因的相關不很簡單...你做一個<%= f.simple_fields_for :players, @roster.players.build do |x| %>這buildes協會常新,當網站的渲染。你想要建立的關聯,只有當它還沒有存在...

<% @roster.players.build if [email protected] %> 
<%= f.simple_fields_for :player do |player_form| %> 
... 
<% end %> 
+0

非常棒的動物!你的代碼最初並沒有起作用,但是你的解釋總是在網站被渲染時建立關聯,這讓我可以修復它。我將在上面的修改中發佈修復。 – mrcolombo 2012-04-24 22:43:51

+0

這個邏輯可以被移動到控制器。您可以將'@ roster.players.build'添加到控制器的'新'操作中。 – 2012-05-18 05:56:57

0

Twitter Bootstrap實際上並沒有驗證任何東西,它只提供styling for validation類(error,success等)。現在

,我不以任何方式一個Ruby專家,但它看起來像你結束你的形式早:

<%= f.simple_fields_for :players, @roster.players.build do |x| %> 
     <%= x.input :first_name %> 
     <%= x.input :last_name %> 
     <%= x.input :active, :as => :hidden, :input_html => {:value => true} %> 
    <%end%> <---- here 
    <%=f.input :class_year %>  
    <%=f.input :jersey_number %> 
    <%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%> 

應該是這個?

<%= f.simple_fields_for :players, @roster.players.build do |x| %> 
     <%= x.input :first_name %> 
     <%= x.input :last_name %> 
     <%= x.input :active, :as => :hidden, :input_html => {:value => true} %>   
     <%= f.input :class_year %>  
     <%= f.input :jersey_number %> 
     <%= f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%> 
    <% end %> 
+0

我知道這是一個樣式驗證,並驗證不處理Twitter的Bootstrap。表單格式正確,它的設置就是[嵌套表單](http://railscasts.com/episodes/196-nested-model-form-part-1)。感謝您查看這個雖然! – mrcolombo 2012-04-21 20:08:02