1

当使用位置响应时,它会忽略验证错误并重定向到指定位置。这是预期的行为吗?

我检查了响应器模块,它检查模型上是否有任何错误。我检查了模型,它在 @solution 对象中包含验证错误。我在这里想念什么?

控制器:

def create
  @problem = Problem.find(params[:problem_id])
  @solution = @problem.solutions.build params[:solution]
  @solution.save
  respond_with(@solution, :location => detail_problem_solution_path(@problem, @solution)
end

模型:

  validates :body, :presence => true, :unless => :reference

参考是真还是假。

4

2 回答 2

1

我今天遇到了这个问题,并在 github 上遇到了这个 Rails 问题。由于路由 url 帮助程序无法为未保存(无效)的记录生成有效的记录,因此似乎引发了异常。

关于允许 procs 作为 location 参数的参数的 github 问题上有讨论,但看起来不会很快添加。

现在我将坚持使用以下解决方案:

def create
  @post = Post.new(params[:post])
  if @post.save
    respond_with(@post, location: edit_post_path(@post))
  else
    respond_with @post
  end
end
于 2012-07-15T23:56:19.890 回答
0

我能够解决的唯一方法是:

  def create
    @problem = Problem.find(params[:problem_id])
    @solution = @problem.solutions.build solution_params
    success = @solution.save
    respond_with(@solution) do |format|
      format.html {redirect_to detail_problem_solution_path(@problem, @solution) } if success
    end
  end
于 2011-11-22T15:13:00.020 回答