2012-07-19 47 views
0

你好我嘗試沒有在佈局中控制器的輸出數據進行排序,使用im設計,formtastic和其他一些寶石,使登錄和用戶界面記錄排序Rails的

的問題是,我有3個表產品,Carted_products和用戶,我想通過Product_id在視圖中顯示Carted_products,我已經通過表中的記錄向他們展示,但是我希望它能將它們分類爲例如:

我有3個不同的命令,其中2人有相同的product_id,他們分別有:

  1. ammount的
  2. PRODUCT_ID
  3. USER_ID

,我可以在模型asociation經由carted_product.product存取權限的價格

繼承人我的視圖代碼:

%TBODY

 - if user_signed_in? 
     - current_user.carted_products.each do |f| 
     - @total = @total + f.carted_product_price(f.ammount, f.product.price) 
     %tr 
      %td.carted_name 
      = f.product.name 
      %td.carted_ammount 
      = f.ammount 
      %td.carted_price 
      $ 
      = f.carted_product_price(f.ammount, f.product.price) 

carted_product_price剛臨危產品的ammount的和價格,並返回多倍的價格

所以它打印在它不事關如果不同的記錄形成相同的產品表中的所有行,我只是想加入他們只有一個錶行輸出

+0

你能詳細說明問題是什麼嗎? – 2012-07-19 18:32:44

+0

好吧,它打印來自指定用戶(「current_user」)的「carted_products」中的所有記錄,這些記錄是通過購買訂單添加的,因此它支付每個訂單,它們無關緊要,因爲它們是同一產品,所以我想加入所有來自單行打印的一個產品的訂單,而不是爲每個訂單打印一行 – 2012-07-19 18:38:10

回答

0

我想你想遍歷產品,而不是carted產品。就我個人而言,我會把這個邏輯的大部分移到模型上。

class Product 
    ... 
    def carted_products_total_amount 
    carted_products.inject(0) {|acc, amount| acc + amount } 
    end 

    def total 
    carted_product_price(carted_products_total_amount, price) 
    end 
end 

然後在視圖:

%tbody 
    - current_user.products.each do |product| 
    - @total = @total + product.total 
    %tr 
     %td.carted_name 
     = product.name 
     %td.carted_amount 
     = product.carted_products_total_amount 
     %td.carted_price 
     $ 
     = product.total 

而且, 「量」 的正確拼寫僅包括一個單一的 「m」 個字符。 ;)

+0

thnx,是的,我解決了它,thnx – 2012-07-20 01:42:42