0

我创建了一个 refinanciamento,但我的节目不正确。

显示.html.erb

<p>
  <strong>Nome:</strong>
  <%= @funcionario.pessoa.nome %>
</p>

<p>
  <strong>Matrícula:</strong>
  <%= @funcionario.matricula %>
</p>

当我输入@funcionario.first.pessoa.nome 时,它​​“工作”,但是,永远返回第一个,我不想要这个,当然。

我的控制器:

  def show
    @funcionario = Funcionario.pesquisa_cpf(params[:pesquisa_func_cpf]).first
    @refinanciamento = Refinanciamento.find(params[:id])
  end

例如,当我注册一个 refinanciamento 时,我显示的 url 是:/refinancimento/100,这个 refinanciamento 具有以下值:id:100,funcionario_id:2 我需要一个显示具有这个 refinanciamento 的 funcionario 的位置。我如何建立这个咨询?

我的模型是:

class Refinanciamento < ActiveRecord::Base
  belongs_to :funcionario
(...)

class Funcionario < ActiveRecord::Base
  has_many :refinanciamentos
(...)

我只想显示创建了 refinanciamento 的 funcionario 的名称和矩阵。谢谢!

4

1 回答 1

2

首先,Rails 是一个固执己见的框架,它假设了一些模式来促进开发人员的生活,比如用英语实现所有东西,从类名到属性。所以你在你的母语中失去了太多的编程(你总是可以I18n一切来翻译文本和东西,代码应该是英文的)。

其次,正确的问题是,您在 和 之间有refinanciamento关联funcionario。如果 a refinanciamento belongs_toa funcionario,当你有一个refinanciamento对象时,你可以使用@refinanciamento.funcionario和获取它的所有数据,比如nomeand matricula

def show
  @refinanciamento = Refinanciamento.find(params[:id])
  # Here you can access funcionario's properties like
  # @refinanciamento.funcionario.pessoa.nome
  # @refinanciamento.funcionario.matricula
  # etc...
end
于 2016-04-08T18:44:50.547 回答