加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails ActiveAdmin:在同一视图中显示相关资源

发布时间:2020-12-16 22:16:01 所属栏目:百科 来源:网络整理
导读:当使用Rails ActiveAdmin gem显示资源时,我想显示另一个关联模型的表. 所以让我们说一个酒庄has_many:产品.现在我想显示与Winery管理员资源的显示页面相关联的产品.而且我希望这是一个类似于产品资源索引的表格. 我得到它的工作,但只有通过手动重新创建HTML
当使用Rails ActiveAdmin gem显示资源时,我想显示另一个关联模型的表.

所以让我们说一个酒庄has_many:产品.现在我想显示与Winery管理员资源的显示页面相关联的产品.而且我希望这是一个类似于产品资源索引的表格.

我得到它的工作,但只有通过手动重新创建HTML结构,哪种吮吸.为相关资源的特定子集创建索引表样式视图是否有更清洁的方法?

我有什么,有点吸吮:

show title: :name do |winery|
  attributes_table do
    row :name
    row(:region) { |o| o.region.name }
    rows :primary_contact,:description
  end

  # This is the part that sucks.
  div class: 'panel' do
    h3 'Products'
    div class: 'attributes_table' do
      table do
        tr do
          th 'Name'
          th 'Vintage'
          th 'Varietal'
        end
        winery.products.each do |product|
          tr do
            td link_to product.name,admin_product_path(product)
            td product.vintage
            td product.varietal.name
          end
        end
      end
    end
  end
end

解决方法

为了解决这个问题,我们使用了partials:

/app/admin/wineries.rb

ActiveAdmin.register Winery do
  show title: :name do
    render "show",context: self
  end
end

应用程序/管理/ products.rb

ActiveAdmin.register Product do
  belongs_to :winery
  index do
    render "index",context: self
  end
end

/app/views/admin/wineries/_show.builder

context.instance_eval  do
  attributes_table do
    row :name
    row :region
    row :primary_contact
  end
  render "admin/products/index",products: winery.products,context: self
  active_admin_comments
end

/app/views/admin/products/_index.builder

context.instance_eval  do
  table_for(invoices,:sortable => true,:class => 'index_table') do
    column :name
    column :vintage
    column :varietal
    default_actions rescue nil # test for responds_to? does not work.
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读