【发布时间】:2014-04-29 19:39:02
【问题描述】:
首先对我的英语和我对 Rails 的了解感到抱歉,我是刚开始的初学者。
我有 2 个有关系的模型:
货物项目:
class CargoItem < ActiveRecord::Base
belongs_to :cargo
attr_accessible :cargo_id, :height, :length, :pieces, :weight, :width
validates :cargo, :height, :length, :width, :weight, :pieces, presence: true
validates :height, :length, :width, :pieces, numericality: { only_integer: true, greater_than_or_equal_to: 1}
validates :weight, numericality: { only_integer: true, greater_than_or_equal_to: 100}
end
货物:
class Cargo < ActiveRecord::Base
belongs_to :airport
belongs_to :user
belongs_to :cargo_state
belongs_to :cargo_price
belongs_to :cargo_description
has_many :cargo_items, :inverse_of => :cargo, :dependent => :destroy
attr_accessible :departure_date, :cargo_state_id, :airport_id, :cargo_price_id, :cargo_description_id, :cargo_items_attributes
accepts_nested_attributes_for :cargo_items, :allow_destroy => true, :reject_if => :all_blank
validates_associated :cargo_items
validates :departure_date, :cargo_state, :airport, :cargo_price, :cargo_description, presence: true
validates :departure_date, date: { after: Proc.new { Date.today - 1.day }, before: Proc.new { Time.now + 1.year } }, :on => :create
default_scope :order => 'departure_date DESC'
end
我使用以下 GEM:simple_forms、nested_forms。这是使用属于 Cargos 的多个 CargoItems(它们有可能被动态添加)添加新 Cargo 的形式:
<%= simple_nested_form_for @cargo, :wrapper => false do |f| %>
<%= f.association :airport, :label_method => :full_airport_name, :value_method => :id , :order => :iata_code %>
<%= f.input :departure_date , as: :date, start_year: Date.today.year,
end_year: Date.today.year + 16,
order: [:day, :month, :year] %>
<%= f.association :cargo_description, :label_method => :description, :value_method => :id, :order => :description %>
<%= f.association :cargo_price, :label_method => :price, :value_method => :id %>
<%= f.association :cargo_state, :label_method => :state, :value_method => :id %>
<hr>
<table>
<tr>
<th><%= :length %></th>
<th><%= :width %></th>
<th><%= :height %></th>
<th><%= :weight %></th>
<th><%= :pieces %></th>
</tr>
<%= f.simple_fields_for :cargo_items, @cargo_item do |cargo_items_fields| %>
<tr class="fields">
<td><%= cargo_items_fields.text_field :length %></td>
<td><%= cargo_items_fields.text_field :width %></td>
<td><%= cargo_items_fields.text_field :height %></td>
<td><%= cargo_items_fields.text_field :weight %></td>
<td><%= cargo_items_fields.text_field :pieces %></td>
<td><%= cargo_items_fields.link_to_remove "Remove this item", :confirm => 'Are you sure you want to remove this item?' %></td>
</tr>
<% end %>
</table>
<%= f.link_to_add "Add a item", :cargo_items %>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
货物管制员:
def new
@cargo = Cargo.new
@cargo_item = CargoItem.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @cargo }
end
end
def create
@cargo = Cargo.new(params[:cargo])
@cargo.user_id = current_user[:id]
respond_to do |format|
if @cargo.save
format.html { redirect_to @cargo, notice: 'Cargo was successfully created.' }
format.json { render json: @cargo, status: :created, location: @cargo }
else
format.html { render action: "new" }
format.json { render json: @cargo.errors, status: :unprocessable_entity }
end
end
end
我的问题是,CargoItems 没有显示验证错误,模型实际上正在验证。我无法使用不符合验证规则的 CargoItems 保存货物。但是如果没有满足验证,Cargo 不会保存,它只是停留在同一页面上,没有任何 CargoItems 字段无效的通知。货物字段验证错误正确显示。
非常感谢您对我的帮助。
【问题讨论】:
标签: ruby-on-rails validation associations nested-forms