我需要一些帮助来正确处理 Twitter Bootstrap 的验证。验证工作正常,问题是 Twitter Bootstrap Flash 屏幕。这就是我的意思:
未填写任何字段并提交退货:http: //i.stack.imgur.com/8pvUc.png
提交的 4 个必填字段中的 2 个返回:http: //i.stack.imgur.com/J6lCi.png (注意它不会显示错误)
我有一个篮球应用程序,其中一个球员可以在许多名册上(用于球员名册存档目的),一个名册可以有很多名球员。
名册.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
播放器.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%>
我在这里先向您的帮助表示感谢!
解决方案
我的问题是每次加载视图时,它都会构建一个新表单,因此永远不会在回滚时恢复错误
我对为嵌套表单构建的控制器进行了更改
控制器
def add_player
...
@new_player = @roster.players.build
end
并根据视图进行相应的更改
_add_player_form.html.erb
<%= f.simple_fields_for :players, @new_player do |x| %>
...
<%end%>
这成功了,谢谢莫伯!