1
我正在開發一個Web應用程序,它必須在主頁上顯示產品列表。 爲此,我有一個ProductsController的:如何爲使用HAML的視圖創建助手?
class ProductsController < ApplicationController
include ProductsHelper
def index
@products = Product.last(6).reverse
end
end
和相應的視圖index.haml:
.main-container.col3-layout
.main
.col-wrapper
.col-main
.box.best-selling
%h3 Latest Products
%table{:border => "0", :cellspacing => "0"}
%tbody
- @products.each_slice(2) do |slice|
%tr
- slice.each do |product|
%td
%a{:href => product_path(:id => product.id)}
= product.title
%img.product-img{:alt => "", :src => product.image.path + product.image.filename, :width => "95"}/
.product-description
%p
%a{:href => "#"}
%p
See all from
%a{:href => category_path(:id => product.category.id)}
= product.category.label
=render "layouts/sidebar_left"
=render "layouts/sidebar_right"
爲了提高我想用助手的這種效率,但我不知道我怎麼能在沒有在products_helper.rb文件中編寫HAML代碼的情況下做到這一點。
有什麼想法,我該如何做到這一點?
我不認爲你可以不醜陋。爲什麼不使用偏分量? –