Twitter Bootstrap Rails:当我点击"NEW"时保存



我在创建成员(表)时遇到问题。每次我点击"新成员",创建一个新成员,它会自动保存一个新成员,就像我点击"提交"一样。每次在调整表单时重新加载页面时,也会创建一个成员。可能是我的控制器打错了。这里是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.

您不应该在新操作中保存成员。您的代码应该看起来像:

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

相关内容

  • 没有找到相关文章