2016-04-04 10 views
0

product,clientおよびdate_rangeの選択があるビューindex.html.erbがあります。これらの値を選択すると、請求書がとなり、new.html.erbとなります。Railsで現在のページ自体に新しいページをレンダリングするには?

請求書コントローラには、対応するテーブルにデータを格納し、次にnew.html.erbをレンダリングするロジックがあります。これは別のページとして提供されます。私は同じページ、つまりインデックスページのインボイステーブルie 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

まず、new.html.erbには作成しないでください。を作成してください。そこにHTMLフォームがあり、提出されると対応するコントローラ(あなたの場合はinvoices_controller)のcreateアクションが呼び出されます。

あなたが請求書を作成し終わったら、新しく作成された請求書(そうInvoicesController#show)にリダイレクトするか、すべてのインスタンスのためのインボイス(InvoicesController#index)ではなく、newページにリダイレクトする必要がありますどちらか。

関連する問題