【问题标题】:Activeadmin where to place this variable code from my viewActiveadmin 从我的视图中将这个变量代码放在哪里
【发布时间】:2015-06-04 07:30:37
【问题描述】:

我创建了一个漂亮的小视图,但其中包含一些我通常会放在控制器中的代码。我尝试为模型制作一个控制器,但 Active Admin 忽略了它。我在 Active Admin 资源文件中尝试了一个小型控制器,它只是导致错误。我可以将它放在哪里,以便 supply_company Active Admin 控制器可以访问它?

<% supply_company_id = SupplyCompany.find(params[:id])
 @ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score) %>

以下部分工作正常(是的,我知道丑陋),但我似乎可以找到将逻辑放置在 ActiveAdmin 中呈现的位置。

渲染部分

/app/views/admin/_supply_company_ratings.html.erb

<%
   supply_company_id = SupplyCompany.find(params[:id])
 @ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score) %>

 <% @ccarrayaverage = (@ccarray.inject 0, :+) / @ccarray.count%>
<% @ccarraypercent = (@ccarrayaverage.to_f)/10*100 %>
<% @divpercent = @ccarraypercent.to_s %>

Average Score
  <h1> <%= @ccarrayaverage %></h1>


Total Score
  <h1>  <%= @ccarray.inject 0, :+ %></h1>

Total Votes
<h1> <%= @ccarray.count %> </h1>

Percent
<h1> <%= @ccarraypercent %> </h1>

<div class="progress">
  <div class="progress-bar  progress-bar-custom" role="progressbar" aria-valuenow="<%=@divpercent%>"
       aria-valuemin="0" aria-valuemax="100" style="width:<%=@divpercent%>%">
    <%= @divpercent.to_s %>% Percent
  </div>
</div>

可能是因为它在侧边栏中没有读取 activeadmin 控制器块。 /app/admin/supply_company.rb

   show title: :company_name do
     attributes_table do
       row :company_name

     end
     end


  sidebar "Products", only: :show do
    attributes_table_for supply_company do

     # row rating_for SupplyCompany, "price"
        row  :company_name
      row "Ratings" do

         render  'supply_company_ratings'
      end

      row :products do |pt|
        pt.products.collect {|c| link_to c.product_name.capitalize, admin_products_path + "\/" + c.id.to_s}.join(", ").html_safe
      end
    end
  end

【问题讨论】:

    标签: ruby-on-rails variables ruby-on-rails-4 controller activeadmin


    【解决方案1】:

    将代码放入 AA 控制器块是正确的想法。

    controller do
      def index
        supply_company_id = SupplyCompany.find(params[:id]).id # note .id part - it makes sure you pass an id not the whole object in the query below
        @ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score)
      end
    end
    

    【讨论】:

    • 干杯。 Dam it 我忘记了控制器块内的 def 索引。谢谢
    • 抱歉试过了,还是不行。我不知道是因为是两种不同的型号还是什么。如果我将它放在 Activeadmin suppy_company.rb 中,它就不会抓取数组。
    • 是的。作为 #<:views::sidebarsection:0xa3a3040> 的索引块 ::undefined method inject' for nil:NilClass . I'm adding this to a sidebar on the show view. As a show block undefined local variable or method supply_company'
    • 你在哪里使用supply_company?据我了解,只有supply_company_id 变量..
    • 唯一的地方是作为activeadmin侧边栏的一部分。有关还包括侧边栏的显示代码,请参见上文。如果我这样放置,但上面的内容不作为侧边栏。 ' 显示标题: :company_name do attributes_table do row :company_name row "Ratings" do render 'supply_company_ratings' end row :products do |pt| pt.products.collect {|c| link_to c.product_name.capitalize, admin_products_path + "\/" + c.id.to_s}.join(", ").html_safe end end end'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多