在 Rails 中提交信息之前出现验证错误?
我有两个模型,Character和Initiative,它们的关系是Character has_one Initiative和Initiative belogns_to Character。我正在对倡议进行验证,并且它正在工作,但问题是在输入任何信息之前为角色创建新的倡议记录时,会出现所有验证错误。有什么想法吗?这是我来自 Initiatives 控制器的代码:
def new
@character = Character.find(params[:character_id])
@initiative = @character.create_initiative(params[:initiative])
end
def edit
@character = Character.find(params[:character_id])
@initiative = @character.initiative
end
def create
@character = Character.find(params[:character_id])
@initiative = @character.create_initiative(params[:initiative])
if @initiative.save
redirect_to character_path(@character), :notice => "initiative successfully created!"
else
render :action => "new"
end
end
def update
@character = Character.find(params[:character_id])
@initiative = @character.initiative
if @initiative.update_attributes(params[:initiative])
redirect_to character_path(@character), :notice => 'Initiative information was successfully updated.'
else
render :action => "edit"
end
end
这是我的模型中的验证本身:
validates_presence_of :dex, :misc, :speed
validates_numericality_of :dex, :misc, :speed
我很确定问题出在创建或新方法中,但我不确定为什么它会在用户输入任何信息之前触发验证。有什么帮助吗?也许不是一个大问题,因为代码正在工作,但我宁愿在实际收到错误之前不显示错误消息。谢谢!
I have two models, Character and Initiative, and their relationship is Character has_one Initiative and Initiative belogns_to Character. I'm working on validation for Initiative, and I have it working, but the issue is all of my validation errors appear when creating a new Initiative record for a Character, before entering any information. Any ideas? Here's my code from Initiatives controller:
def new
@character = Character.find(params[:character_id])
@initiative = @character.create_initiative(params[:initiative])
end
def edit
@character = Character.find(params[:character_id])
@initiative = @character.initiative
end
def create
@character = Character.find(params[:character_id])
@initiative = @character.create_initiative(params[:initiative])
if @initiative.save
redirect_to character_path(@character), :notice => "initiative successfully created!"
else
render :action => "new"
end
end
def update
@character = Character.find(params[:character_id])
@initiative = @character.initiative
if @initiative.update_attributes(params[:initiative])
redirect_to character_path(@character), :notice => 'Initiative information was successfully updated.'
else
render :action => "edit"
end
end
And here's the validation itself from my model:
validates_presence_of :dex, :misc, :speed
validates_numericality_of :dex, :misc, :speed
I'm pretty sure the problem lies in the create or new methods, but I'm not sure why it's triggering the validation before a user enters any information. Any help? Maybe not a huge concern, since the code IS working, but I'd rather not display an error message before actually getting an error. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在新操作中使用 build_initiative 而不是 create_initiative 吗?当向用户发送要创建对象的表单时,无需保存对象。此外,如果你的角色有一个主动性,他只能有一个,所以我怀疑 AR 是否会欣赏你尝试创建另一个主动性。
请参阅http://guides.rubyonrails.org/association_basics.html#has_one-association-参考
shouldn't you be using build_initiative instead of create_initiative in your new action ? no need to save an object when sending to the user a form that intends to create it. Moreover, if your character has_one initiative, he can only have one so i doubt AR appreciates that you try to create another.
see http://guides.rubyonrails.org/association_basics.html#has_one-association-reference