Rails中处理无效表单提交的正确方法 [英] The correct way to handle invalid form submissions in Rails

查看:43
本文介绍了Rails中处理无效表单提交的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,不确定我是否同意我在我经历过的一些教程中所做的事情.这个问题与如何处理无效的表单提交有关.做事的标准方式似乎是:

I'm new to rails and am not sure I agree with the way I've things done in some of the tutorials I've gone through. The issue has to do with how to handle invalid form submissions. The standard way of doing things seem to be:

class ThingsController < ApplicationController


  # POST /things
  def create

    @thing = Thing.new(params[:thing])

    if @thing.save
      flash[:notice] = 'Thing created'
      redirect_to(@thing)
    else
      render :action => :new
    end

  end

当@thing.save 失败时,用户会看到相同的表单,其中预先填写了他刚刚输入的值,以及出现问题的闪现.到目前为止一切都很好,除了现在 URL 已从/things/new 更改为 things/,人们期望它呈现索引视图.

When @thing.save fails, the user is presented with the same form, prefilled-out with the values he just entered, along with a flash of what went wrong. So far so good, except that now the URL has changed from /things/new to things/, which one would expect to be rendering the index view instead.

此外,如果用户刷新页面,他现在正在查看索引视图.如果他点击返回,系统会提示他重新提交表单,我一直试图避免这种情况.如果我redirect_to(new_thing_path),用户之前的提交就会丢失,错误信息也是如此.

Also, if the user refreshes the page, he is now looking at the index view. If he clicks back, he is prompted to resubmit the form, which I've always tried to avoid. If I redirect_to(new_thing_path), the user's previous submission is lost, as are the error messages.

我意识到在 REST 方面,这种方法可能是正确的",因为创建一个事物对象应该是向/things 发布的结果,但在用户界面方面,我并不特别关心它.

I realize that RESTfully, this method may be "correct", since creation of a thing object should be the result of POSTing to /things, but user-interface-wise, I don't particularly care for it.

我可以在用户会话中手动"保存无效的@thing 对象,在我将他重定向回 new_thing_path 后显示,但这感觉就像一个黑客.似乎应该有一种轨道方式"来做到这一点.

I could "manually" save the invalid @thing object in the user's session, to be displayed after I redirect him back to new_thing_path, but that feels like a hack. It seems like there should be a "rails way" of doing just that.

想法?

推荐答案

正如您所发现的,默认情况下,当您指定 resources :things 时,创建新事物的 POST 路径位于 <代码>/事物.以下是 rake 路由 的输出:

As you've found, by default when you specify resources :things, the POST path for creating a new thing is at /things. Here's the output for rake routes:

    things GET    /things(.:format)          {:action=>"index", :controller=>"things"}
           POST   /things(.:format)          {:action=>"create", :controller=>"things"}
 new_thing GET    /things/new(.:format)      {:action=>"new", :controller=>"things"}
edit_thing GET    /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"}
     thing GET    /things/:id(.:format)      {:action=>"show", :controller=>"things"}
           PUT    /things/:id(.:format)      {:action=>"update", :controller=>"things"}
           DELETE /things/:id(.:format)      {:action=>"destroy", :controller=>"things"}

听起来你想要更像这样的东西:

It sounds like you want something more like this:

create_things POST   /things/new(.:format)      {:action=>"create", :controller=>"things"}
       things GET    /things(.:format)          {:action=>"index", :controller=>"things"}
    new_thing GET    /things/new(.:format)      {:action=>"new", :controller=>"things"}
   edit_thing GET    /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"}
        thing GET    /things/:id(.:format)      {:action=>"show", :controller=>"things"}
              PUT    /things/:id(.:format)      {:action=>"update", :controller=>"things"}
              DELETE /things/:id(.:format)      {:action=>"destroy", :controller=>"things"}

虽然不推荐,但你可以通过以下路线得到这个结果:

Although not recommended, you can get this result with the following route:

resources :things, :except => [ :create ] do
  post "create" => "things#create", :as => :create, :path => 'new', :on => :collection
end

您还需要修改您的表单,使它们 POST 到正确的路径.

You would also need to modify your forms to make them POST to the correct path.

综上所述,您在问题中对网址的描述听起来不正确.您列出以下内容:提交新的 thing(在 /things/new 提交表单)后,

All that being said, the description of the URLs you have in your question don't sound right. You list the following: After submitting a new thing (submitting a form at /things/new),

  1. URL 从 /things/new 变为 /things
  2. 点击返回提示重新提交表单
  3. 刷新显示things#index

不是我在自己的 Rails 3 应用程序中体验到的功能.相反,我发现:在提交新的 thing(在 /things/new 提交表单)后,

This is not the functionality I experience in my own Rails 3 applications. Instead, I find that: After submitting a new thing (submitting a form at /things/new),

  1. URL 从 /things/new 变成了 /things(这个是一样的)
  2. 点击返回会将用户返回到提交的表单(无需重新发布)
  3. 刷新提示以重新提交表单(在我看来正如预期的那样)
  1. The URL changes from /things/new to /things (this is the same)
  2. Clicking back takes the user back to the non-submitted form (no request for a re-post)
  3. Refreshing prompts to resubmit the form (as expected in my opinion)

这篇关于Rails中处理无效表单提交的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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