嵌套路线值不填充编辑导轨



我在 maprow之间具有belongs_to的关系。 map has_many rows

出于某种原因,当我尝试编辑一行时,即使在数据库中填充了一行时,当我尝试编辑此行时,它也不会显示值。为什么会这?以下是_form.html.erbedit.html.erb文件。

edit.html.erb

<%= render 'form', row: @row %>

_form.html.erb

<%= form_for [@map, @map.rows.build], method: :post, url: map_rows_path do |form| %>
  <div class="field">
    <%= form.label :timeframe %>
    <%= form.text_field :timeframe, id: :timeframe %>
  </div>
  <div class="field">
    <%= form.label :standards %>
    <%= form.text_field :standards %>
  </div>
  <div class="field">
    <%= form.label :content %>
    <%= form.text_field :content %>
  </div>
  <div class="field">
    <%= form.label :skills %>
    <%= form.text_field :skills %>
  </div>
  <div class="field">
    <%= form.label :resources %>
    <%= form.text_field :resources %>
  </div>
  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

如果我在导轨控制台中尝试Row.last,它将确认该行带有数据。我假设这与表单的设置方式有关,但是我不熟悉这些关系。如何修复?

看来您正在将行传递到 <%= render 'form', row: @row %>的形式,但是在实际编辑表单中,您使用的是变量,@map并通过说@map.rows.build来构建空白行,因此行对象将是一个具有任何值的新对象。

根据您的代码,将其更改为以下并查看。(我假设您正在控制器中的编辑操作中设置@map对象)

<%= form_for [@map, row], method: :post, url: map_rows_path do |form| %>

最新更新