在管理员视图中按用户分组记录 [英] Grouping records by user in Admin view

查看:50
本文介绍了在管理员视图中按用户分组记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以管理员身份登录时使用他们的用户名获取所有记录.为此,我的控制器代码如下;

I would like to get all records with their usernames when signed in as Admin. For that my controller code is as below;

amol361s_controller.rb

amol361s_controller.rb

    def index

        if current_user.admin
          @amol361s = Amol361.all.search(params[:search]).order("created_at ASC")
          @users = User.all.amol361s
        else
          @amol361s = current_user.amol361s.all.search(params[:search]).visible.order("created_at ASC") 
        end

        respond_to do |format|
          format.html
          format.js
        end

    end

index.html.erb

index.html.erb

<tbody id = "kola">              
<% @users.each do |user| %>
<%= user.amol361s.each do |amol361| %>

<tr class="tr-<%= cycle('odd', 'even') %>" id='<%= "tr_#{amol361.id}" %>'>


<% if current_user && current_user.admin %>
<td class="col-1"><%= link_to amol361.date.try(:strftime,'%d/%m/%Y'), edit_amol361_path(amol361) %></td>
<% else %>
<td class="col-1"><%= amol361.date.try(:strftime,'%d/%m/%Y') %></td>
<% end %>

<td class="col-3"><%= span_with_possibly_red_color amol361.description %></td>


<td class="col-1"><%= number_with_precision(amol361.amount, :delimiter => ",", :precision => 2) %></td>

<td class="col-1 neg"><%= number_with_precision(amol361.discount, :delimiter => ",", :precision => 2) %></td>

<td class="col-1 neg"><%= number_with_precision(amol361.paid, :delimiter => ",", :precision => 2) %></td>


<% @balance += amol361.amount.to_f - amol361.discount.to_f - amol361.paid.to_f %>

<% color = @balance >= 0 ? "pos" : "neg" %>

<td class="col-1 <%= color %>"><%= number_with_precision(@balance.abs, :delimiter => ",", :precision => 0) %></td>

<td class="col-1"><%= amol361.delndel %></td>

<td class="col-1"><%= amol361.remark %></td>

<% if current_user && current_user.admin %>
<td class="col-1"><%= link_to "Hide", hide_amol361_path(amol361), method: :put, remote: true, style: "color:#bb7272;" %>
<%= link_to amol361, method: :delete, data: { confirm: "Are you sure?" }, :class => 'delete_item' do %>
<i class="fa fa-trash" ></i>
<% end %>
</td>
<% end %>

</tr>

<% end %>
<% end %>


</tbody>

现在我收到错误为 undefined method amol361s' for # User::ActiveRecord_Relation:0x00007f5dec19d088.

因此,我尝试获取记录后跟用户名的记录,类似于我们根据月份收集记录的方式.

So, I am trying to get the records followed by usernames, similar to how we gather records based on month.

欢迎提出任何建议.

提前致谢.

推荐答案

据我所知,如果当前用户是管理员,您可以简单地预先加载用户记录,如下所示:

From what I understand, you can simply eager load user records if the current user is admin, like this:

@amol361s = Amol361.includes(:user).search(params[:search]).order("created_at ASC")

如果你这样做,通过调用这个关系的每个成员的关联来获取用户数据将是有效的,就像这样:

If you do this, it will be effective to fetch the user data simply by calling association on each member of this relation, like this:

# example:
@amol361s.first.user.username # or whatever the name of username column is

我不确定我是否正确理解了您的问题,因为我有点不清楚.

I'm not sure I understood your question correctly though, cause it's a little bit unclear to me.

这篇关于在管理员视图中按用户分组记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆