2015-10-27 77 views
0

我想在導軌中創建發票。發票可以有物品,每個物品都會有數量,稅金&價格。這是我們每天看到的典型發票。導軌進銷存應用程序

爲了創建發票什麼是最好的辦法。

什麼是發票和項目的共同模式?

我知道的項目將是一個單獨的模型。但是,我們如何纔能有一個發票視圖,這會創建發票和添加到它的項目?

我的意思是,一個新的發票頁面中,會出現客戶名單,和項目的名單,但在這裏我不知道如何讓足協當我創建發票。有沒有我可以遵循的一個很好的例子?

請欣賞一些幫助。甚至只是通過我需要爲了實現這個目標需要遵循的步驟散步...

這裏是我的基本ERD

enter image description here

回答

0

相當寬泛的問題,這裏就是我想要做的:

#app/models/invoice.rb 
class Invoice < ActiveRecord::Base 
    belongs_to :user 

    has_many :line_items 
    has_many :items, through: :line_items 

    accepts_nested_attributes_for :line_items 
end 

#app/models/line_item.rb 
class LineItem < ActiveRecord::Base 
    belongs_to :invoice 
    belongs_to :item 
end 

#app/models/item.rb 
class Item < ActiveRecord::Base 
    belongs_to :company 

    has_many :line_items 
    has_many :invoices, through: :line_items 
end 

- -

#app/models/user.rb 
class User < ActiveRecord::Base 
    has_many :invoices 
end 

這將是基準級「發票」關聯結構 - 您的可以在其上構建/users

你的路線等可以如下:

#config/routes.rb 
resources :invoices 

#app/controllers/invoices_controller.rb 
class InvoicesController < ApplicationController 
    def new 
     @invoice = current_user.invoices.new 
     @invoice.line_items.build 
    end 

    def create 
     @invoice = current_user.invoices.new invoice_params 
     @invoice.save 
    end 
end 

那麼你的看法會是這樣的:

#app/views/invoices/new.html.erb 
<%= form_for @invoice do |f| %> 
    <%= f.fields_for :line_items do |l| %> 
    <%= f.text_field :quantity %> 
    <%= f.collection_select :product_id, Product.all, :id, :name %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

這將創建相應的@invoice,與您就可以打電話如下:

@user.invoices.first 

除此之外,我沒有足夠的地方具體信息可幫助明確

+0

謝謝你的反饋,我喜歡你迄今爲止的建議。我會嘗試這個,一旦我得到它的工作,或者如果我有任何問題(希望不是),會讓你知道。乾杯 –

+0

Jus想知道應該在項目表和line_item表內進行什麼。我的意思是item_name價格數量&ect。就像我的情況一樣,我喜歡current_company和調用現有的項目,我會這樣current_company.items,所以我需要更改爲current_company.line_items?我對物品和line_item以及它們內部的東西感到困惑。 –

0

我可以推薦使用payday gem?我已經在過去的應用程序中創建了發票模型,並且我會告訴你什麼,它有時可能會非常棘手,具體取決於您正在構建的應用程序的類型。但除了便利因素之外,我喜歡使用這款寶石的原因是它也可以將您的發票作爲可定製的PDF進行呈現。

這使得將項目添加到發票微風爲好,例如從他們的GitHub頁面:

invoice = Payday::Invoice.new(:invoice_number => 12) 
invoice.line_items << Payday::LineItem.new(:price => 20, :quantity => 5, :description => "Pants") 
invoice.line_items << Payday::LineItem.new(:price => 10, :quantity => 3, :description => "Shirts") 
invoice.line_items << Payday::LineItem.new(:price => 5, :quantity => 200, :description => "Hats") 
invoice.render_pdf_to_file("/path/to_file.pdf") 
+0

感謝您的建議:)我試圖建立這種不使用大量的寶石。 –