0

一个“产品”有许多“并行产品”:

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 as JS”和我的语法状态“ProductsController#index as HTML”。

在帮助程序中创建“has_many”关系列表视图链接的正确 ActiveScaffold 语法是什么?

4

1 回答 1

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 += ", &hellip; (#{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

您将需要为 css 'empty' 类做一个小的 'hack',例如:

.active-scaffold a.empty {
  display: block;
  text-align: center;
}

注意:这适用于 Rails/AS 3.1 或更高版本。

于 2012-06-06T08:05:56.297 回答