从我的角度来看,Activeadmin 在哪里放置这个变量代码
Activeadmin where to place this variable code from my view
我创建了一个不错的小视图,但其中包含一些我通常会放在控制器中的代码。我尝试为模型制作控制器,但 Active Admin 忽略了它。我已经尝试在 Active Admin 资源文件中执行一个小控制器 do 块,这只是导致了错误。我可以把它放在哪里以便 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
将代码放入控制器块是正确的想法。
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
我创建了一个不错的小视图,但其中包含一些我通常会放在控制器中的代码。我尝试为模型制作控制器,但 Active Admin 忽略了它。我已经尝试在 Active Admin 资源文件中执行一个小控制器 do 块,这只是导致了错误。我可以把它放在哪里以便 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
将代码放入控制器块是正确的想法。
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