shree
shree

Reputation: 1

How to render a new page in the current page itself in Rails?

I hava a view index.html.erb in which the selection of product,client and date_range is given. On selecting those values, an invoice is created which is given in new.html.erb.

In invoices controller, I have logic to store data in corresponding tables and then render new.html.erb. It comes as a separate page. I need to have invoice table ie., new.html.erb in the same page ie., index page.

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  

Now, I want the new page table(invoice table) within the index page. How to accomplish this?

Upvotes: 0

Views: 95

Answers (1)

born4new
born4new

Reputation: 1687

First of all, the new.html.erb should not create anything. You should have an html form in there that, when submitted, will call the create action on the corresponding controller (in your case, invoices_controller).

Once you're done with creating the invoice, you should either redirect to the newly created invoice (so the InvoicesController#show) or redirect to all the invoices for instance (InvoicesController#index), but not the new page.

Upvotes: 1

Related Questions