如何让 Rails build 和 fields_for 只创建一个新记录而不包括现有记录? [英] How to get Rails build and fields_for to create only a new record and not include existing?

查看:23
本文介绍了如何让 Rails build 和 fields_for 只创建一个新记录而不包括现有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 buildfields_foraccepts_nested_attributes_for 在与新注册(有许多注册注意事项).太好了.

I am using build, fields_for, and accepts_nested_attributes_for to create a new registration note on the same form as a new registration (has many registration notes). Great.

问题:在现有注册的编辑表单上,我想要创建另一个新的注册说明,但我不想看到每个现有注册说明的字段.

Problem: On the edit form for the existing registration, I want another new registration note to be created, but I don't want to see a field for each of the existing registration notes.

我有这个

class Registration < ActiveRecord::Base
  attr_accessible :foo, :bar, :registration_notes_attributes
  has_many :registration_notes
  accepts_nested_attributes_for :registration_notes
end

还有这个

class RegistrationsController < ApplicationController
  def edit
    @registration = Registration.find(params[:id])
    @registration.registration_notes.build
  end
end

在视图中我正在这样做:

and in the view I am doing this:

<%= form_for @registration do |r| %>
  <%= r.text_field :foo %>
  <%= r.text_field :bar %>
  <%= r.fields_for :registration_notes do |n| %>
    <%= n.text_area :content %>
  <% end %>
<% end %>

并且它正在为新的注册说明(好)该注册的每个现有注册说明创建一个空白文本区域(不,谢谢).

and it is creating a blank text area for a new registration note (good) and each existing registration note for that registration (no thank you).

有没有办法只为该注册创建一个新的笔记,而保留现有的笔记?

Is there a way to only create a new note for that registration and leave the existing ones alone?

推荐答案

EDIT:我之前的回答(见下文)让我感到困扰,因为它不是很好(它仍然循环遍历所有其他<代码>registration_notes 不必要).阅读 API 多一点后,获得 OP 想要的行为的最佳方法是替换:

EDIT: My previous answer (see below) was bugging me because it's not very nice (it still loops through all the other registration_notes needlessly). After reading the API a bit more, the best way to get the behaviour the OP wanted is to replace:

<%= r.fields_for :registration_notes do |n| %>

与:

<%= r.fields_for :registration_notes, @registration.registration_notes.build do |n| %>

fields_for 可选地采用第二个参数,该参数是要传递给构建器的特定对象 (查看 API),它是内嵌的.在控制器中而不是在表单中创建和传递新注释实际上可能更好(只是为了将逻辑移出视图).

fields_for optionally takes a second parameter which is the specific object to pass to the builder (see the API), which is built inline. It's probably actually better to create and pass the new note in the controller instead of in the form though (just to move the logic out of the view).

原始答案(我非常接近):

只是澄清一下,您希望您的编辑表单包含一个新的嵌套注册备注(并忽略任何其他现有注册备注)?我还没有测试过这个,但你应该可以通过替换来做到这一点:

Just to clarify, you want your edit form to include a new nested registration note (and ignore any other existing ones)? I haven't tested this, but you should be able to do so by replacing:

  <%= r.fields_for :registration_notes do |n| %>

与:

  <%= r.fields_for @registration.registration_notes.build do |n| %>

编辑:好的,我自己的快速测试不起作用,但您可以这样做:

EDIT: Okay, from a quick test of my own that doesn't work, but instead you can do:

<%= r.fields_for :registration_notes do |n| %>
  <%= n.text_area :content if n.object.id.nil? %>
<% end %>

这只会在注册笔记的id为nil(即尚未保存)时添加文本区域.

This will only add the text area if the id of the registration note is nil (ie. it hasn't been saved yet).

此外,我实际上首先对此进行了测试,并且确实有效;)

Also, I actually tested this first and it does work ;)

这篇关于如何让 Rails build 和 fields_for 只创建一个新记录而不包括现有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆