Rails在另一个控制器中显示数据

时间:2014-10-15 13:15:09

标签: ruby-on-rails-4 controller relationship custom-routes

我需要在中央控制器中显示其他控制器的数据 中心= patients_controller.rb
其他= weights_controller.rb
控制器有这样的关系 型号:
patient.rb = has_many:体重
weight.rb = belongs_to:病人

我需要仅显示一名患者的体重 我试试这个:
创建route = get'peso /:id /',以:'patients#weight'
在控制器患者中创建一个def =

def weights  
end  

在此之前,我试着打电话来看看:

<%= @patient.weight %>  

但我无法证明任何人都可以帮助我的结果。

1 个答案:

答案 0 :(得分:1)

我找到了一个显示数据的解决方案 在患者控制器中我做这个

在控制器患者中

  def weight  
  end  

并创建了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´

并在index.html.erb

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

在routes.rb

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

解决了我的问题