1

我对此感到有些愚蠢,但是:

if @prof.update_attributes(params[:profile])
      respond_to do |format|
        format.html {redirect_to(@prof, :notice => "Profile successfully created.") }
      end
    end

...在我的控制器的更新方法中。我在模型中验证了一些属性。

如果验证失败,我只想让他们回到同一个表单上,被各种红色文本责骂。(即错误数组中的所有内容)。

验证失败时出现“模板缺失”错误 - 用于“更新”的模板。我觉得我忽略了一些非常简单的事情。帮助!

4

2 回答 2

2

尝试这个:

respond_to do |format|
  if @prof.update_attributes(params[:profile])
    format.html { redirect_to(@prof, :notice => "Profile successfully created.") }
  else
    format.html { render action: 'edit' }
  end
end
于 2013-10-31T14:02:42.557 回答
1

错误的原因是,除非另有说明,否则 Rails 将尝试呈现与操作同名的模板,在这种情况下update,显然不存在。

您要做的是告诉 railsedit在发生错误时再次呈现操作。通常,您会对块执行此操作respond_to,允许块根据验证通过或失败做出不同的响应。

目前,您的 if 语句包装了该块,并且没有语句告诉 rails 在发生错误时以不同的方式呈现。要解决此问题,我将执行以下操作:

respond_to do |format|
    if @prof.update_attributes(params[:profile])
        # all is well, redirect as you already wrote
    else
        format.html { render action: 'edit' }
    end
end
于 2013-10-31T14:12:39.493 回答