如何在Rails中在当前页面中呈现新页面?

时间:2016-04-04 10:04:44

标签: html ruby-on-rails render

我有一个视图index.html.erb,其中提供了productclientdate_range的选择。选择这些值后,会在new.html.erb中显示帐单

发票控制器中,我有逻辑将数据存储在相应的表中,然后呈现new.html.erb。它是一个单独的页面。我需要在同一页面中有发票表即。new.html.erb,即索引页。

if params[:commit] == 'Create Invoice'
  @invoice = Invoice.new(invoice_params)
  @client_id = params[:invoices][:client_id]
  @project_id = params[:invoices][:project_id]
  @from = params[:invoices][:fromdate]
  @to = params[:invoices][:todate]
  @totalhrs = params[:invoices][:totalbilledhrs]
  @timesheet = Timesheet.where("client_id = ? and project_id = ? 
and is_billed= 'true' and timesheetdate between ? 
and ?",@client_id,@project_id,@from,@to)

  if @invoice.save 


  end
  @task = @timesheet.uniq.pluck("task_id");
  @task.each do |t|
    @invoice_task = InvoiceTaskDetail.new
    @invoice_task.invoice_id = Invoice.last.id
    @invoice_task.task_id = t
    @invoice_task.task_name = Task.find(t).task_name
    @invoice_task.totalbilledhrs = 0
    @invoice_task.totalamt = 0
    if @invoice_task.save
    end

    @timesheet1 = @timesheet.all.where("task_id=?",t)
    @subtask = @timesheet1.uniq.pluck("role_id")
    @subtask.each do |sub|
    @invoice_subtask = InvoiceSubtask.new
    @invoice_subtask.invoice_task_detail_id =
    InvoiceTaskDetail.last.id
    @invoice_subtask.role_id = sub
    @invoice_subtask.role_description =
   Role.find(sub).role_description
    @timesheet2 = @timesheet1.all.where("role_id = ?",sub)
    @timesheet2.select("role_id,sum(hours) as hrs, sum(rate) as
   totrate").group("role_id").each do |tt|
    @invoice_subtask.billedhrs =tt.hrs
    @invoice_subtask.billedrate = tt.totrate

    @invoice_subtask.totalamount = tt.hrs * tt.totrate
    end

    if @invoice_subtask.save
    end

    end

    InvoiceSubtask.where("invoice_task_detail_id
  =?",InvoiceTaskDetail.last.id).select("invoice_task_detail_id,
  sum(billedhrs) as totalhrs,sum(totalamount) as
  tot").group("invoice_task_detail_id").each do |ttt|

    @invoice_task.update_attribute(:totalbilledhrs,ttt.totalhrs)
    @invoice_task.update_attribute(:totalamt,ttt.tot)
    end





  end

  InvoiceTaskDetail.where("invoice_id =
  ?",Invoice.last.id).select("invoice_id,sum(totalbilledhrs) as
  total,sum(totalamt) as tot").group("invoice_id").each do |tt1|
    @invoice.update_attribute(:totalbilledhrs, tt1.total)
    @invoice.update_attribute(:invoiceamt, tt1.tot)
  end

    @invoice_tasks =
  InvoiceTaskDetail.where("invoice_id=?",@invoice.id)
     render 'new'
else
  @invoices = Invoice.all
  @client_id = params[:invoices][:client_id]
  @project_id = params[:invoices][:project_id]
  @from = params[:invoices][:fromdate]
  @to = params[:invoices][:todate]

  @invoice = @invoices.where("client_id= ? and project_id = ? and 
  fromdate = ? and todate = ?", @client_id, @project_id, @from, 
  @to).last
  if @invoice.nil?
    flash[:error] = "Sorry!! No Invoices available for the given    
  client/project/date range"
    redirect_to invoices_path

  else
      @invoice_tasks = InvoiceTaskDetail.where("invoice_id = 
      ?",@invoice.id)

      render 'new'
  end
end  

现在,我想在索引页面中使用新的页面表(发票表)。 如何做到这一点?

1 个答案:

答案 0 :(得分:1)

首先,new.html.erb不应该创建任何内容。你应该有一个html表单,当提交时,会在相应的控制器上调用create操作(在你的情况下,invoices_controller)。

完成创建发票后,您应该重定向到新创建的发票(以及InvoicesController#show)或重定向到所有发票(例如InvoicesController#index),但不是new页面。