在选择标签中显示当前用户的数据及其名称 [英] Show current User's data and it's name in select tag

查看:53
本文介绍了在选择标签中显示当前用户的数据及其名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据通过导航栏传递的参数值显示当前用户(经理)的数据,并且它在选择标签中的名称,用户有两个角色('user'、'manager')或枚举角色(0、1).这是我在导航栏中给出的链接 -

I want to display the current user's (manager) data based on params value passed through navbar and it's name in select tag, User has two role ('user', 'manager') or enum role (0 , 1). This is the link I have given in navbar -

%li= link_to 'Timesheet', is_manager? ? timesheet_path(user_id: current_user.id) : timesheet_path, class: navlink_class('timesheets')

但是在控制器中它变得越来越复杂,因为我使用的查询是这些 -

But in the controller it is getting complicated as the queries which I have used are these -

    @begin_date = params[:begin_date].present? ? Date.parse(params[:begin_date]) : Date.today.beginning_of_month
    @end_date = params[:end_date].present? ? Date.parse(params[:end_date]) : Date.today.end_of_month
    begin_datetime = @begin_date.beginning_of_day 
    end_datetime = @end_date.end_of_day 
    @user_tasks = Task.joins(:task_time_trackers).where("(task_time_trackers.created_at BETWEEN ? AND ?)",begin_datetime, end_datetime ).distinct.order("id ASC")
    user_trackers = TaskTimeTracker.joins(:task).where("(task_time_trackers.created_at BETWEEN ? AND ?)",begin_datetime, end_datetime )
    u_id = nil
    u_id = params[:u_id] if (params[:u_id] && params[:u_id] != 'all')
    u_id = current_user.id if current_user.role == 'user'
    unless u_id.nil? 
      @user_tasks = @user_tasks.where( task_time_trackers: { user_id: u_id})
      user_trackers = user_trackers.where( task_time_trackers: { user_id: u_id})
    end

最后是我使用的 select 标签 -

And finally the select tag which I have used -

select_tag(:u_id, options_for_select(User.user_for_select(current_user), selected: params[:u_id]), class: "input-sm form-control", onchange: "this.form.submit();")

用户模型 -

      class << self
        def user_for_select(user)
          self
            .pluck("CASE WHEN id = #{user.id} THEN 'Me' ELSE name END AS name, id")
            .unshift(['All', 'all'])
        end
      end

推荐答案

这就是我得到的结果,在导航栏中做了这个改变 -

This is how I got the result, in the navbar made this change -

- if is_manager?
  %li= link_to 'Timesheet', timesheet_path(u_id: current_user.id) 
- else
  %li= link_to 'Timesheet', timesheet_path

并在选择标签中进行此更改 -

and in the select tag this change -

select_tag(:u_id, options_for_select(User.user_for_select(current_user), params[:u_id]), class: "input-sm form-control", onchange: "this.form.submit();")

这篇关于在选择标签中显示当前用户的数据及其名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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