我有两个模型,Issue
和ProductionReport
。我想创建一个包含Issue
和ProductionReport
字段的表单。 Issues
的值来自数据库,ProductionReport
的值是新的。最后,当用户点击提交按钮时,所有值都应存储在ProductionReport
表中。我有以下控制器:
def index_productionReport
@issue = Issue.all
@issue = Issue.paginate(page: params[:page], per_page:10).order("created_at DESC")
end
def add_productionReport
@production_report=ProductionReport.new
@issue=Issue.find(params[:id])
end
def get_production
@issue = Issue.find(params[:id])
@production_report = ProductionReport.new(production_report_params)
@production_report.save
flash[:notice] = "production Saved Successfully!!!"
redirect_to :action => "index_productionReport"
end
我的观点是:
<%= form_for @production_report, :url=>{:controller=>"users",:action=>"get_production" } do |i|%>
<%= fields_for @issue do |i| %>
<tr><td><%= i.text_field :order_no,:required => true,:autofocus=>true %></td></tr>
<tr><td><%= i.text_field :issue_slip_no,:required => true %></td></tr>
<%end%>
<tr><td><%= i.label :finished_goods_name,"finished goods name"%></td></tr>
<tr><td><%= i.text_field :finished_goods_name,:required => true %></td></tr>
<tr><td><%= i.label :total_no_of_items_produced,"total no of item"%></td></tr>
<tr><td><%= i.text_field :total_no_of_items_produced,:required => true %></td></tr>
<tr><td><%= i.label :weight_per_item,"weight per item" %></td></tr>
<tr><td><%= i.text_field :weight_per_item,:required => true %></td></tr>
<tr><td><%= i.label :total_weight_consumed,"total weight consumed" %></td></tr>
<tr><td><%= i.text_field :total_weight_consumed,:required => true %></td></tr>
<%= i.submit "save" %>
<%end%>
有人请帮助我实现我的需要..
更新: 流: 我在index_productionReport上有问题列表,每个问题都有“添加”链接。当用户单击add时,add_productionRepor将显示来自Issue表的2个值,剩余的空字段将用于ProductionReport。现在,当用户单击“保存”按钮时,所有值都应存储在ProductionReport表中。
答案 0 :(得分:0)
根据您提供的信息,ProductionReport
属于Issue
,Issue
可能有ProductionReport
个。在这种情况下,我会将issue_id
存储在production_reports
表中,这被视为标准化方式,而不是某些信息(order_no
和issue_slip_no
),这被视为一种非规范化的方式。
无论Issue
对于给定ProductionReport
我需要哪些信息,我都可以直接从Issue
查询。例如:@production_report.issue.order_no
或@production_report.issue.issue_slip_no
。如果您想隐藏实施细节,您甚至可以委派order_no
和issue_slip_no
,如下所示:
class Issue < ActiveRecord::Base
has_many :production_reports
end
class ProductionReport < ActiveRecord::Base
belongs_to :issue
delegate :order_no, :issue_slip_no, :to => :issue
end
de-normalized way
会导致重复,从而导致数据一致性。想象一下,有人在创建order_no
后更新issue_slip_no
的{{1}}和Issue
。它们都包含两种不同的信息。
尽管如此,如果您需要ProductionReport
order_no
直接在issue_slip_no
存储Issue
,由于某些其他业务要求,您可以使用以下内容视图:
ProductionReport
最后,我不会将所有操作推到<%= form_for @production_report, :url=>{:controller=>"users", :action=>"get_production" } do |i|%>
<tr><td><%= i.text_field :order_no, :required => true, :readonly => true, value => @issue.order_no, :autofocus=>true %></td></tr>
<tr><td><%= i.text_field :issue_slip_no, :readonly => true, value => @issue.issue_slip_no, :required => true %></td></tr>
<tr><td><%= i.label :finished_goods_name, "finished goods name" %></td></tr>
<tr><td><%= i.text_field :finished_goods_name, :required => true %></td></tr>
<tr><td><%= i.label :total_no_of_items_produced,"total no of item" %></td></tr>
<tr><td><%= i.text_field :total_no_of_items_produced,:required => true %></td></tr>
<tr><td><%= i.label :weight_per_item,"weight per item" %></td></tr>
<tr><td><%= i.text_field :weight_per_item,:required => true %></td></tr>
<tr><td><%= i.label :total_weight_consumed,"total weight consumed" %></td></tr>
<tr><td><%= i.text_field :total_weight_consumed,:required => true %></td></tr>
<%= i.submit "save" %>
<%end%>
,而是按照以下方式进行操作:
UsersController