我有一个Rails 3应用程序,在表单提交(有错误)后,它将转到显示页面。
编辑页面上的网址:
http://localhost:5000/costprojects/509/edit
带有提交声明的表单页面:
<%= simple_form_for @costproject, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
...
<%= f.submit 'Submit to Division', :class => 'btn btn-warning' %>
控制器代码:
respond_to do |format|
if @costproject.update_attributes(params[:costproject])
flash[:success] = "Project Submitted" if @costproject.previous_changes.include?(:submit_date)
format.html { redirect_to nextpath }
format.json { render json: @costproject }
else
format.html { render action: "edit" }
format.json { render json: @costproject.errors, status: :unprocessable_entity }
end
end
提交错误后,网址为:
http://localhost:5000/costprojects/509
为什么不呢:
http://localhost:5000/costprojects/509/edit
我检查其他表格,这似乎是正常的 - 但是,为什么?
答案 0 :(得分:0)
在客户端提交表单时确定URL,默认显示已编辑的记录。成功或失败是在服务器端决定的,因此即使您呈现编辑表单,URL也保持不变。您可以做的是将故障时客户端重定向回编辑页面,这意味着再往返一次。或者,您可以使用AJAX提交表单,将用户保留在编辑表单上,直到答案可能会成功将其重定向到显示页面。
答案 1 :(得分:0)
尝试更改控制器以进行更新,然后检查错误。如果@costproject.save
失败,只需渲染编辑视图。
@costproject.update_attributes(params[:costproject])
if @costproject.save
..
else
format.html { render :edit }
..
end