【问题标题】:form_for, fields_for and two modelsform_for、fields_for 和两个模型
【发布时间】:2011-03-07 22:46:48
【问题描述】:

我有创建两个对象并将它们保存到数据库的表单。

我想做接下来的事情

  • 在数据库中保存数据(展位对象)
  • 验证字段(我在模型中进行了验证)
  • 如果验证失败,我想用输入的数据填充字段
  • 编辑此表单的操作

问题

  • 如果我使用@report,我会得到:

为 nil 调用 id,这将 误为4个错误

(找不到对象)。我在控制器中,在 encreate action @report = ReportMain.new 和呈现该视图的操作中。

  • 当我使用 :report_main(模型名称)时,它可以工作,它将数据保存到数据库中,但是当验证失败时我无法填充字段。

问题:

  • 如何处理这两个模型以使其发挥作用(验证、填充字段、编辑)?
  • 如果方法不对,您能给我一些建议吗?

我的观点是这样的:

<%= form_for(@report, :url => {:action => 'encreate'}) do |f| %>

<%= render "shared/error_messages", :target => @report %>

<%= f.text_field(:amount) %>


<% fields_for @reporte do |r| %>
    <%= r.check_box(:q_pripadnost) %>Pripadnost Q listi


    <%= select_tag('nacinpakovanja',options_for_select([['Drveno bure', 'Drveno bure'], ['Kanister', 'Kanister'], ['Sanduk', 'Sanduk'], ['Kese', 'Kese'], ['Posude pod pritiskom', 'Posude pod pritiskom'], ['Kompozitno pakovanje', 'Kompozitno pakovanje'], ['Rasuto', 'Rasuto'], ['Ostalo', 'Ostalo']])) %> 

    <%= r.text_field(:ispitivanjebroj) %>
    <%= r.text_field(:datumispitivanja) %>


<% end %>


<input id="datenow" name="datenow" size="30" type="text" value="<%= @date %>">

<div class="form-buttons">
       <%= submit_tag("Unesi izvestaj") %>
</div>

在 ReportController 中创建肌动蛋白:

def encreate

    @report = ReportMain.new
    @reporte = ReportE.new
    @reportparam = params[:report_main]


    @report.waste_id = params[:waste][:code]
    @report.warehouse_id = Warehouse.find_by_user_id(current_user.id).id
    @report.user_id = current_user.id
    @report.company_id = current_user.company_id
    @report.amount = @reportparam[:amount]
    @report.isimport = false
    @report.isfinished = false
    @report.reportnumber =  ReportMain.where(:company_id => current_user.company_id, :isimport => false).count.to_i+1
    if @report.save
      @reporte.report_main_id = @report.id
    else
      redirect_to(:action => 'exportnew')
      return
    end

    @reporte.vrstaotpada = params[:vrstaotpada]
    @reporte.nacinpakovanja = params[:nacinpakovanja]
    @reporte.ispitivanjebroj = @reportparam[:ispitivanjebroj]
    @reporte.datumispitivanja = @reportparam[:datumispitivanja]
    @reporte.q_pripadnost = @reportparam[:q_pripadnost]
    @reporte.datumpredaje = @date

    if @reporte.save
      redirect_to(:action => 'show', :id => @reporte.id)
    else
      redirect_to(:action => 'exportnew')
    end
  end

【问题讨论】:

  • 你解决了吗?如何?我也有同样的情况。

标签: ruby-on-rails ruby-on-rails-3 forms


【解决方案1】:

我认为您在这种情况下的问题是您使用redirect_to 而不是渲染。当您使用 redirect_to 时,您会丢失当前操作中的所有变量。我可能会在你的 encreate 动作中做这样的事情:

if @reporte.save
  render :show
else
  render :exportnew
end

当您使用渲染时,它将使用当前操作中的变量,但您发送到渲染方法的操作中的视图。因此,当使用 @report 变量调用 form_for 时,它已经填充了发送给 encreate 的值。只需确保您在不同的操作中使用相同的变量名,但看起来您已经这样做了。

【讨论】:

  • 我在控制器中进行了更改,但我得到了同样的结果,我调用了 nil 对象:|
  • 它在抱怨哪一行?
  • 我想我已经弄清楚了如何做到这一点,我会在这里做详细的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多