如何在Rails的数据表中显示名称而不是user_id

问题描述 投票:0回答:1

所有人...几天来,我的应用程序遇到了一个问题,这让我思考了很多:我有一个名为“设备”的数据表,其中包含对“用户”的引用,在我的数据表中,我要带回“ user_id”,但我想在后端获取该数据并在前端显示“用户名”。如何获得预期的结果?您能给我的任何帮助,我将不胜感激

在“用户”列中托管“ user_id”columns

在我的index.html.haml中:

.table__wrapper
    = form_tag edit_multiple_devices_path, method: :get do
      %table{:role => 'datatable'}
        %thead
          %tr
            %th
            %th.abbr{:title => "aidi"} Id
            %th.abbr{:title => "imei"} Imei
            %th.abbr{:title => "brand"} Marca
            %th.abbr{:title => "model"} Modelo
            %th.abbr{:title => "prov"} Proovedor
            %th.abbr{:title => "fac"} Factura
            %th.abbr{:title => "din"} Entrada
            %th.abbr{:title => "sim"} Sim
            %th.abbr{:title => "iccid"} Iccid
            %th.abbr{:title => "status"} Status
            %th.abbr{:title => "user_id"} Usuario
            %th.abbr{:title => "client"} Cliente
            %th.abbr{:title => "dout"} Salida
            %th.abbr{:title => :use} Usado
            %th.abbr{:title => :old} Reutilizado
          %tbody
            - @devices.each do |device|
              %tr
                %td= check_box_tag "device_ids[]", device.id
                %td= device.aidi
                %td= device.imei
                %td= device.brand
                %td= device.model
                %td= device.prov
                %td= device.fac
                %td= device.din
                %td= device.sim
                %td= device.iccid
                %td= device.status
                %td= device.user_id
                %td= device.client
                %td= device.dout
                %td= device.use
                %td= device.old
      = submit_tag "Editar", class: "button is-primary is-rounded"

在我的device.rb中

class Device < ApplicationRecord
belongs_to :user
attr_accessible :aidi, :imei, :brand, :model, :prov, :fac, :din, :sim, :iccid, :status, :user_id, :client, :dout, :use, :old
end

在我的user.rb中

class User < ApplicationRecord
has_many :devices
end

感谢您的知识

ruby-on-rails ruby ruby-on-rails-3 datatable devise
1个回答
0
投票
- @devices.includes(:user).each do |device|
  ...
  %td= device.user.name
© www.soinside.com 2019 - 2024. All rights reserved.