0

我已經創建了一個很好的小視圖,但它有一些代碼,我通常會放在控制器中。我曾嘗試爲模型製作控制器,但Active Admin忽略它。我已經嘗試了一個小的控制器在Active Admin資源文件中造成錯誤。我可以在哪裏放置這樣以使supply_company Active Admin控制器可以訪問它?Activeadmin在哪裏放置我的視圖中的這個可變代碼

<% 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="<%[email protected]%>" 
     aria-valuemin="0" aria-valuemax="100" style="width:<%[email protected]%>%"> 
    <%= @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 

回答

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 
+0

乾杯。大壩它已經忘記了控制器塊內的def index。謝謝 –

+0

對不起,嘗試過它仍然無法工作。如果它因爲是兩個不同的模型或者什麼,我不會發誓。如果我把它放在Activeadmin suppy_company.rb它只是不會抓住數組。 –

+0

你有什麼錯誤嗎? –