lundi 2 mars 2020

How to display name instead of user_id in a datatable in Rails

everybody... For a couple of days I have encountered a problem in my application and that is giving me a lot to think about: I have a data table called "Devices" that contains a reference to "Users", in my data table I am bringing back the "user_id" but I want to take that data in the backend and show the "username" in the frontend. How can I get the expected result? I would greatly appreciate with any help you can give me

in the "user" column the "user_id" is hosted columns

in my 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"

in my 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

in my user.rb

class User < ApplicationRecord
has_many :devices
end

thanks for the knowledge

1 commentaire:

  1. Do you understand there's a 12 word phrase you can speak to your partner... that will trigger intense feelings of love and instinctual attraction for you buried within his chest?

    Because hidden in these 12 words is a "secret signal" that fuels a man's impulse to love, idolize and protect you with his entire heart...

    12 Words That Trigger A Man's Love Response

    This impulse is so hardwired into a man's genetics that it will drive him to work better than ever before to to be the best lover he can be.

    Matter of fact, triggering this mighty impulse is absolutely binding to achieving the best ever relationship with your man that once you send your man one of these "Secret Signals"...

    ...You will immediately find him open his mind and soul for you in a way he haven't experienced before and he will perceive you as the only woman in the galaxy who has ever truly interested him.

    RépondreSupprimer