在Rails 3中,当资源创建操作失败并调用render:new时,为什么URL必须更改为资源的索引url? [英] In Rails 3, when a resource create action fails and calls render :new, why must the URL change to the resource's index url?

查看:147
本文介绍了在Rails 3中,当资源创建操作失败并调用render:new时,为什么URL必须更改为资源的索引url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Books的资源。



我有一个新的动作,给新视图的标准:

  @book = Book.new 



在我的控制器中:

  @book = Book.create 
...#一些逻辑
如果@ book.save
redirect_to book)
else
render:new
end

漂亮的标准;以及使用render:new的原因是,对象被传递回视图,并且可以报告错误,重新填充表单项等。



,除了每次我返回到表单(通过render:new),我的错误会显示出来,但我的网址是INDEX网址,这是

  / books 

而不是

  / books / new 

在那里我开始在第一名。我已经看到几个其他帖子这个问题,但没有答案。至少,一个人会认为它会在/ books / create,我也有一个视图文件(在这种情况下与新的)。



我可以这样做:

  t saved then 
flash [:error] =错误!
redirect_to new_book_path

但是随后的@book数据丢失,



为什么是render:新登陆我在/ books,我的索引动作,当正常的URL调用

实际上是 发送给您的创建的路径。它在 create 操作中,其路径为 / books ,使用HTTP方法POST。这看起来与索引路径 / books 相同,但索引路径使用HTTP方法GET。 rails路由代码在确定要调用哪个操作时会考虑方法。验证失败后,您仍然处于创建操作,但是您正在呈现视图。这有点混乱,但是像 render:new 这样的行实际上并不调用新的动作;它仍然在运行创建动作,它告诉Rails渲染新的视图


I have a resource called Books. It's listed as a resource properly in my routes file.

I have a new action, which gives the new view the standard:

@book = Book.new

On the model, there are some attributes which are validated by presence, so if a save action fails, errors will be generated.

In my controller:

@book = Book.create
...  # some logic
if @book.save
  redirect_to(@book)
else
  render :new
end

This is pretty standard; and the rationale for using render:new is so that the object is passed back to the view and errors can be reported, form entries re-filled, etc.

This works, except every time I'm sent back to the form (via render :new), my errors show up, but my URL is the INDEX URL, which is

/books

Rather than

/books/new

Which is where I started out in the first place. I have seen several others posts about this problem, but no answers. At a minimum, one would assume it would land you at /books/create, which I also have a view file for (identical to new in this case).

I can do this:

# if the book isn't saved then
flash[:error] = "Errors!"
redirect_to new_book_path

But then the @book data is lost, along with the error messages, which is the entire point of having the form and the actions, etc.

Why is render :new landing me at /books, my index action, when normally that URL calls the INDEX method, which lists all the books?

解决方案

It actually is sending you to the create path. It's in the create action, the path for which is /books, using HTTP method POST. This looks the same as the index path /books, but the index path is using HTTP method GET. The rails routing code takes the method into account when determining which action to call. After validation fails, you're still in the create action, but you're rendering the new view. It's a bit confusing, but a line like render :new doesn't actually invoke the new action at all; it's still running the create action and it tells Rails to render the new view.

这篇关于在Rails 3中,当资源创建操作失败并调用render:new时,为什么URL必须更改为资源的索引url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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