【问题标题】:Submit rails form with a field not in model提交带有不在模型中的字段的 rails 表单
【发布时间】:2016-11-07 18:42:19
【问题描述】:

我需要提交一个表单,其中包含一个不属于该模型的选择。保存操作后,我需要此值用于其他操作,但我收到错误:

undefined method `tecnico' for #<Interventi:0x9802cf8>

这是我的表格的一部分:

<%= form_for :interventi, url: welcome_nuovointerventosalva_path do |f| %>

<div class="row">


    <div class="col-md-2">
        <%= f.label :data, 'Data creazione' %>
  <div class="form-group input-group">
    <span class="input-group-addon"> <i class="fa fa-calendar"></i> </span>
    <%= f.text_field :data, :class => "date-picker form-control input-mini", readonly: true, :value => Time.now.strftime("%Y/%m/%d")  %>
    <!--<span class="help-block">formato YYY-MM-DD</span> -->
  </div>
    </div>

    ....................................
    ....................................

    <div class="col-md-3">
        <%= f.label 'Tecnico' %>
        <div class="form-group input-group">
            <%= f.select :tecnico, options_for_select(@tecnici.collect{ |tec| [tec.nome, tec.id] }), {}, class: 'form-control search-select' %>
        </div>
    </div>

这是控制器

 def nuovointervento
@titolo = "Nuovo Intervento"
@interventi = Interventi.new
@clienti = Clienti.all.order(:nome)
@categorie = Categorie.all
@tecnici = Utenti.where(operatore: '1').order(:nome)

结束


private
def parametri_intervento
  params.require(:interventi).permit(:cliente_id, :data, :intervento, :note, :chiuso, :codice, :operator_id, :monteore, :categoria, :referente)
end

结束


是的,'tecnico' 字段不属于 Interventi 模型,但在后期操作中,保存后,我需要此值用于其他记录保存,使用 tecnico_id 和创建的 'intervento' id(它是一个连接表)

我该如何解决?

谢谢你!

【问题讨论】:

标签: ruby-on-rails forms


【解决方案1】:

抱歉,正如 MageeWorld 所说,这篇文章是重复的。

答案 Form field not part of rails db model 解决了我的问题!

谢谢大家

【讨论】:

    【解决方案2】:

    @EffeBi 允许在您的控制器中使用此属性,就像其他属性一样,例如:数据

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多