简体   繁体   English

Rails 3中RESTful表单的问题

[英]problem with RESTful forms in Rails 3

okay, so basically, I have a normal form for my model: 好的,基本上,我的模型有一个正常形式:

= form_for @operator do |f|
blah blah blah

In my operators controller, i have this: 在我的操作员控制器中,我有以下内容:

def new
  @operator = Operator.new
  @operator.build_user

  respond_to do |format|
    format.html {}
  end
end

def create
  @user = User.create(params[:operator].delete(:user))
  @user.update_attributes(:login => @user.email)
  @operator = Operator.new(params[:operator].merge(:user => @user))

  respond_to do |format|
    if @operator.save
      format.html {redirect_to new_operator_aircraft_path(@operator)}
    else
      format.html { render :action => "new", :error => @operator.errors }
    end
  end
end

very basic stuff. 非常基本的东西。 I have some validates_presence_of stuff in my model so naturally when I submit my form, it should show me that I have errors(and keep the fields I have filled up) 我的模型中有一些validates_presence_of东西,所以当我提交表单时,很自然地,它应该告诉我我有错误(并保留我填写的字段)

Right so far? 到目前为止正确吗? yeah. 是的 The problem is, it seems I am posting to /operators and that's what renders. 问题是,似乎我正在发布到/ operators,这就是渲染结果。 I seem to have forgotten about what happens in Rails2.3+ but shouldn't I be redirected to /operators/new again? 我似乎已经忘记了Rails2.3 +中会发生什么,但是我不应该再次重定向到/ operators / new吗? or was that the intended behavior all along? 还是一直以来的预期行为?

Here's what I think you are asking: 我想这就是您要问的:

After I submit a form with errors, why does the URL read "/operators" rather than "/operators/new". 在提交带有错误的表单之后,为什么URL读为“ / operators”而不是“ / operators / new”。

Thanks to resourceful routing, when submitting a form via POST to "/operators" the create action is called on the OperatorsController. 由于资源丰富的路由,当通过POST向“ / operators”提交表单时,将在OperatorsController上调用create动作。 If you encounter errors when saving your operator, you've instructed the controller to render the new action within the same request. 如果在保存运算符时遇到错误,则已指示控制器在同一请求中呈现新动作。

render :action => "new", :error => @operator.errors

This means a redirect is not occurring and therefore the URL remains "/operators". 这意味着没有发生重定向,因此URL仍为“ / operators”。

If a redirect were to occur, you would lose all the state information of the @operator object in the current request, including the errors you encountered as well as the form values you just submitted. 如果发生重定向,您将丢失当前请求中@operator对象的所有状态信息,包括遇到的错误以及刚提交的表单值。

In other words, working as intended. 换句话说,按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM