我正在尝试在order
模型中创建表单。我一直在想。我可以在创建新订单时在order_id
文本字段中显示新的order_number
吗?我试试
<%= form_for(@order, html: {class: 'form form-horizontal'}) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="table table-hover">
<tr>
<td><%= f.label :staff_id %>
</td>
<td><%= f.collection_select :staff_id, Staff.all, :id, :staff_name, {:prompt => false}%>
</td>
</tr>
<tr>
<td><%= f.label :customer_id %>
</td>
<td><%= f.collection_select :customer_id, Customer.all, :id, :customer_name, {:prompt => false}%>
</td>
</tr>
<tr>
<td><%= f.label :service_id %>
</td>
<td><%= f.collection_select :service_id, Service.all, :id, :service_type, {:prompt => false}%>
</td>
</tr>
<tr>
<td><%= f.label :order_number %>
</td>
<td><%= f.text_field :order_number , :value => @order.id %>
</td>
</tr>
<tr>
<td><%= f.label :order_vehicle %>
</td>
<td><%= f.text_field :order_vehicle %>
</td>
</tr>
<tr>
<td><%= f.label :order_plate %>
</td>
<td><%= f.text_field :order_plate %>
</td>
</tr>
<tr>
<td><%= f.label :order_description %>
</td>
<td><%= f.text_field :order_description %>
</td>
</tr>
<tr>
<td><%= f.label :order_damage %>
</td>
<td><%= f.text_field :order_damage %>
</td>
</tr>
<tr>
<td><%= f.label :order_deposit %>
</td>
<td><%= f.number_field :order_deposit %>
</td>
</tr>
<tr>
<td><%= f.label :order_price %>
</td>
<td><%= f.number_field :order_price %>
</td>
</tr>
<tr>
<td><%= f.label :order_date %>
</td>
<td><%= f.date_select :order_date %>
</td>
</tr>
<tr>
<td><%= f.label "List of task" %>
</td>
<td><%= f.collection_check_boxes :task_ids, Task.all, :id, :task_name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</td>
</tr>
<tr>
<td>
</td>
<td><%= f.submit %>
</td>
</tr>
</table>
<% end %>
在创建新订单时的表单中没有显示任何内容。
id
仅在我编辑order
时出现。
答案 0 :(得分:0)
id
。它应该是数据库表中的主键,并且自动增加。在我的选项中,订单号应由系统生成,用户无法对其进行修改。
您可以添加after_create
观察者来设置order_number
class Order < ActiveRecord::Base
after_create :set_order_number
def set_order_number
self.update_attribute('order_number', self.id)
end
end