0

我在创建成员(用于表)时遇到问题。每次我点击“新成员”,创建一个,它会自动保存一个新成员,就好像我点击了“提交”一样。每次我在调整表单时重新加载页面时,也会创建一个成员。这可能是我的控制器上的错字。这是'def new'。

def new
  @member = Member.new
  @member.association_id = @association.id
  @member.save
  3.times { @member.submembers << Submember.new() }
end

否则,某些脚本可能会出错。问题是,我该如何解决这个问题?

谢谢你。

如果需要任何其他信息,请在此处评论。

错误信息:

Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1     id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter    either is not present or the key from which it is accessed is incorrect.
4

1 回答 1

0

您不应该在新操作中保存成员。您的代码应如下所示:

def new
  @member = Member.new      
  3.times { @member.submembers << Submember.new() }
end

并且表单应该提交以创建操作,您可以在其中使用 Association_id 保存模型:

def create
  @member = Member.new(params[:member])
  @member.association_id = @association.id
  if @member.save
    redirect_to members_path, :notice => "Member was successfully created"
  else
    render :new
  end
end
于 2013-01-07T20:35:57.783 回答