当表单在 Rails 中验证失败时,会出现一个奇怪的 NoMethodError。我可以毫无问题地访问“新”方法来第一次填写表格。但是随后表单提交到嵌套控制器,如果验证失败,它会尝试再次重定向到“新”(应该如此)但抛出undefined method articles_path
.
我认为这可能与嵌套有关,但我相信这里的设置对于加载嵌套的“新”路径是正确的?
我确定我遗漏了一些明显的东西......但是为什么会发生这种情况?
控制器:
def new
@user = User.find(current_user)
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @article }
end
end
def create
@article = current_user.articles.build(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to root_path, :flash => { :notice =>
"Article added!" } }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status =>
:unprocessable_entity }
end
end
end
形式:
= form_for([@user, @article], :html => { :multipart => true }) do |f|
- if @article.errors.any?
#errorExplanation
%h2
= pluralize(@article.errors.count, "error")
prohibited this post from being saved:
%ul
- @article.errors.full_messages.each do |msg|
%li= msg
.clearfix
= f.label :title, "Article title *"
.input
= f.text_field :title