Ruby on rails 从另一个模型访问值,而不在Rails的当前模型中创建字段

Ruby on rails 从另一个模型访问值,而不在Rails的当前模型中创建字段,ruby-on-rails,Ruby On Rails,我有两种型号,Issue和ProductionReport。我想创建一个表单,其中包含问题和生产报告的字段。问题的值将来自数据库,生产报告的值是新的。最后,当用户单击submit按钮时,所有值都应该存储在ProductionReport表中。我有以下控制器: def index_productionReport @issue = Issue.all @issue = Issue.paginate(page: params[:page], per_page:10).order("cr

我有两种型号,
Issue
ProductionReport
。我想创建一个表单,其中包含
问题
生产报告
的字段。
问题的值将来自数据库,
生产报告的值是新的。最后,当用户单击submit按钮时,所有值都应该存储在
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%>
{:controller=>“users”,:action=>“get_production”}do | i |%>
真,:自动对焦=>真%>
正确%>
正确%>
正确%>
正确%>
正确%>
请有人帮我满足我的需要

更新: 流量:
我在index_productionReport上有问题列表,每个问题都有“添加”链接。当用户单击add时,将显示add_productionRepor,其中包含来自Issue表的2个值,剩余的空字段用于ProductionReport。现在,当用户单击save按钮时,所有值都应该存储在ProductionReport表中

根据您提供的信息,
ProductionReport
似乎属于
Issue
并且
Issue
可以有许多
ProductionReport
s。在这种情况下,我会将
问题id
存储在
生产报告
表中,这被认为是一种规范化的方式,而不是一些信息(
订单号
问题单号
),这被认为是一种非规范化的方式

对于给定的
生产报告
,无论我需要从
问题
获得什么信息,我都可以直接从
问题
查询。例如:
@production\u report.issue.order\u no
@production\u report.issue.issue\u slip\u 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 
最后,我将遵循以下分离,而不是将所有操作推入
userscoontroller

class UsersController < ApplicationController
  # include actions pertain to users model
end

class IssuesController < ApplicationController
  def index
    @issue = Issue.all
    @issue = @issue.paginate(page: params[:page], per_page:10)
    @issue = @issue.order("created_at DESC")
  end
end

class ProductionReportsController < ApplicationController
  def new
    @issue = Issue.find(params[:id])
    # if you were to use belongs_to and has_many design, I mentioned above
    @production_report = @issue.production_reports.build
    # or not 
    @production_report = ProductionReport.new
  end

  def create
    @issue = Issue.find(params[:id])
    # if you were to use belongs_to and has_many design, I mentioned above
    @production_report = @issue.production_reports.build(production_report_params)
    # or not
    @production_report = ProductionReport.new(production_report_params)

    if @production_report.save
      flash[:notice] = "production Saved Successfully!!!"
      redirect_to issues_url
    else
      # logic for failed save, repopulate the form with validation errors
    end    
  end
end 
class UsersController
为什么要将用户输入字段用于从数据库读取的值(@issue)?是否要将
@issue
订单号
出库单号
与用户输入一起保存在
@production\u report
?您的
production\u report\u params
方法是什么样子的?定义production\u report\u params.require(:production\u report)。允许!End在输入ProductionReport的值之前,用户应查看出库模型的订单号和出库单号。。问题模型中的所有值都将处于只读模式@flips那么为什么要使用输入字段,而不只是在屏幕上打印?至于params方法:我从未使用过
permit,您是否尝试过参数require(:生产报告)。permit(:产成品名称,:生产项目总数,:每项重量,:消耗的总重量)
我希望问题模型中的字段与生产报告字段一起存储。这是我的主要问题。。
class UsersController < ApplicationController
  # include actions pertain to users model
end

class IssuesController < ApplicationController
  def index
    @issue = Issue.all
    @issue = @issue.paginate(page: params[:page], per_page:10)
    @issue = @issue.order("created_at DESC")
  end
end

class ProductionReportsController < ApplicationController
  def new
    @issue = Issue.find(params[:id])
    # if you were to use belongs_to and has_many design, I mentioned above
    @production_report = @issue.production_reports.build
    # or not 
    @production_report = ProductionReport.new
  end

  def create
    @issue = Issue.find(params[:id])
    # if you were to use belongs_to and has_many design, I mentioned above
    @production_report = @issue.production_reports.build(production_report_params)
    # or not
    @production_report = ProductionReport.new(production_report_params)

    if @production_report.save
      flash[:notice] = "production Saved Successfully!!!"
      redirect_to issues_url
    else
      # logic for failed save, repopulate the form with validation errors
    end    
  end
end