'产品'有很多'parallel_products':
class Product < ActiveRecord::Base
has_many :parallel_products, :class_name => "Product", :foreign_key => "master_product_id"
end
在控制器中,我将'parallel_products'列添加到列表视图中:
class ProductsController < ApplicationController
active_scaffold :product do |config|
config.list.columns = [ :parallel_products ]
end
end
这为我提供了列表视图中的ActiveScaffold生成链接,以查看,创建和编辑所选产品的并行产品。
到目前为止一切顺利。
现在,我想在帮助程序中指定此'parallel_products'链接。链接本身没有变化,它应该与ActiveScaffold生成的链接完全一样。原因是我需要添加一个条件,以便仅在某些情况下显示链接。
ActiveScaffold生成的链接在日志中如下所示:
Started GET "/products?assoc_id=6&association=parallel_products&eid=products_6_parallel_products&parent_scaffold=products&adapter=_list_inline_adapter" for 172.16.99.11 at 2012-03-05 09:37:45 +0100
Processing by ProductsController#index as JS
Parameters: {"assoc_id"=>"6", "association"=>"parallel_products", "eid"=>"products_6_parallel_products", "parent_scaffold"=>"products", "adapter"=>"_list_inline_adapter"}
我对助手中ActiveScaffold has_many关系链接的最佳建议是:
link_to("link text", :controller => "products", :assoc_id => record.id, :association => "parallel_products", :eid => "products_#{record.id}_parallel_products", :parent_scaffold => "products", :adapter => "_list_inline_adapter")
这给了我日志:
Started GET "/products?adapter=_list_inline_adapter&assoc_id=6&association=parallel_products&eid=products_6_parallel_products&parent_scaffold=products" for 172.16.99.11 at 2012-03-05 09:39:38 +0100
Processing by ProductsController#index as HTML
Parameters: {"adapter"=>"_list_inline_adapter", "assoc_id"=>"6", "association"=>"parallel_products", "eid"=>"products_6_parallel_products", "parent_scaffold"=>"products"}
我的链接不起作用,但似乎非常接近。唯一的区别是生成的链接状态'ProductsController #index为JS',我的语法状态'ProductsController #index为HTML'。
在帮助程序中创建'has_many'关系列表视图链接的正确ActiveScaffold语法是什么?
答案 0 :(得分:0)
感谢Sergio Cambra帮助解决这个问题。
如果放在帮助器中,这是'has_many'关联链接的语法:
link_to("link text", {:controller => "products", :association => "parallel_products",
:parent_scaffold => "products", :product_id => record.id}, :class => 'index as_action',
:id => "as_products-index-parallel_products-#{record.id}-link",
:remote => true, :data => {:position => :after, :action => 'index'})
要完整地回答这个问题,可以通过以下方式来完全替换自动生成的AS关联链接:
def parallel_products_column(record)
if product.parallel_products.blank?
link_text = "-"
css_class = "index as_action empty"
else
link_text = product.parallel_products[0...3].map{ |p| p.name }.join(", ")
if product.parallel_products.length > 3
link_text += ", … (#{product.parallel_products.length})"
end
css_class = "index as_action"
end
link_to(link_text.html_safe, {:controller => "products", :association => "parallel_products",
:parent_scaffold => "products", :product_id => record.id}, :class => css_class,
:id => "as_products-index-parallel_products-#{record.id}-link",
:remote => true, :data => {:position => :after, :action => 'index'})
end
你需要一个小'hack'用于css'empty'类,例如:
.active-scaffold a.empty {
display: block;
text-align: center;
}
注意: 这适用于Rails / AS 3.1或更新版本。