即使在验证错误时,response_with 也会重定向到指定位置,在 rails3 中 [英] respond_with is redirecting to specified location even on validation errors, in rails3

查看:24
本文介绍了即使在验证错误时,response_with 也会重定向到指定位置,在 rails3 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When using location in respond with, it is ignoring validation errors and redirecting to the specified location. Is this expected behavior?

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

I checked in the responder module that it checking if there are any errors on the model. I inspected the model and it contains validation errors in the @solution object. What am I missing here?

控制器:

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

引用是真还是假假.

推荐答案

我今天遇到了这个问题,遇到了 github 上的这个 Rails 问题.由于路由 url 助手无法为未保存(无效)记录生成有效记录,因此似乎抛出了异常.

I encountered this problem today, and come upon this Rails issue over at github. The exception seems to be thrown since the route url helper can't generate a valid for unsaved (invalid) records.

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

There's discussion on the github issue about allowing procs as an argument to the location parameter, but it doesn't look like it'll be added anytime soon.

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

For now I'll stick with using the following solution:

def create
  @post = Post.new(params[:post])
  if @post.save
    respond_with(@post, location: edit_post_path(@post))
  else
    respond_with @post
  end
end

这篇关于即使在验证错误时,response_with 也会重定向到指定位置,在 rails3 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆