【问题标题】:Consult don't work how I want咨询不要按我的意愿工作
【发布时间】:2016-04-09 02:44:50
【问题描述】:

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

show.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 的功能的地方。我如何构建这个咨询?

我的模型是:

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

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

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

【问题讨论】:

  • "我只想显示已创建 refinanciamento 的函数的名称和矩阵。"您可能需要提供一些翻译来为读者提供上下文。
  • 抱歉,我的系统是葡萄牙语的,因为我不会翻译,好吗?是“nome”和“matricula”
  • 我猜nome是“name”,“matricula”、“pesquisa_cpf”和“Consult”是什么意思?
  • 矩阵可以“注册”; pesquisa_cpf 可以是“search_cpf”,咨询就是咨询。
  • 我相信consultquerypesquisa_cpf 是一种根据文档编号搜索人员的方法。

标签: ruby-on-rails ruby ruby-on-rails-4 rails-4-2-1


【解决方案1】:

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

第二个问题,也就是问题所在,refinanciamentofuncionario 之间存在关联。如果refinanciamentobelongs_tofuncionario,当您有refinanciamento 对象时,您可以使用@refinanciamento.funcionario 并获取其所有数据,例如nomematricula

def show
  @refinanciamento = Refinanciamento.find(params[:id])
  # Here you can access funcionario's properties like
  # @refinanciamento.funcionario.pessoa.nome
  # @refinanciamento.funcionario.matricula
  # etc...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多