Search code examples
ruby-on-rails-4controllerrelationshipcustom-routes

Rails Show data in another controller


I need to show data of other controller in my central controller
Central = patients_controller.rb
Other = weights_controller.rb
Controllers have relationship like this
MODELS:
patient.rb = has_many :weight
weight.rb = belongs_to :patient

I need to show only weights of one patient in view
I try this:
Create route = get 'peso/:id/', to: 'patients#weight'
create a def in controller patients =

def weights  
end  

Before this i try to call in view this:

<%= @patient.weight %>  

But i cant show any result anyone can help me.


Solution

  • I have found a solution to show data
    in patient controller i make this

    in controller patients

      def weight  
      end  
    

    And created view weight.html.slim

     p#notice = notice
        .row
          p
          table
    thead
    tr
        th Nome:
        th Sexo:
        th Data de nascimento:
        th Idade:
    
    
           tbody
      tr
        td = @patient.name
        td = @patient.sex.name
        td = @patient.born
        td = ((Date.today - @patient.born) / 365).to_i
          p
    
          table
    thead
      tr
        th Plano:
        th Senha de internação:
        th Admimissão:
        th Altura:
    tbody
      tr
        td = @patient.plan.name
        td = @patient.pass
        td = @patient.admission
        - if @patient.weight.first.inch != nil
            td = @patient.weight.first.inch
        - else
            td = 'Paciente sem altura registrada'
          p
          table
            thead
              tr
                th Data
                th Peso
                th IMC:
    
            strong pesos:
    tbody
      - @patient.weight.each do |m|
        tr
          - if m.date != nil
              td = m.date
          - else
              td = 'Sem pesos registrados'
    
          - if m.weight != nil
              td  = m.weight
          - else
              td  = 'sem peso registrado'
    
          - if @patient.weight.first.inch != nil
              td  = (m.weight / (@patient.weight.first.inch**2).to_f).round(2)
          - else
              td  = 'sem dados para este calculo'
    
          br´
    

    and in index.html.erb

    <td><a href="/peso/<%= patient.id %>" target="_blank">Relatorio</a></td>  
    

    In routes.rb

    get 'peso/:id/', to: 'patients#weight'  
    

    And solved my problem