Ruby on rails 3.1 资源创建失败后的错误路由

Ruby on rails 3.1 资源创建失败后的错误路由,ruby-on-rails-3.1,Ruby On Rails 3.1,具有此控制器: def create @company = Company.new(params[:company]) respond_to do |format| if @company.save format.html { redirect_to @company, notice: 'Company was successfully created.' } format.json { render json: @company, status: :created, loc

具有此控制器:

def create
@company = Company.new(params[:company])
respond_to do |format|
  if @company.save
    format.html { redirect_to @company, notice: 'Company was successfully created.' }
    format.json { render json: @company, status: :created, location: @company }
  else
    format.html { render action: "new" }
    format.json { render json: @company.errors, status: :unprocessable_entity }
  end
end
end
有人知道为什么在尝试保存未通过验证的记录后,rails会导致“新建”操作,但浏览器栏中显示的url是索引(即/companys)的路径吗


干杯

当您发布到
/companys
时,会发生
创建
操作。当它失败时,它不会重定向到新页面,而是将
new
操作呈现到位。这是Rails的REST操作的标准

在更新记录时,您将看到类似的行为。编辑表单将向公司资源(例如,
/companys/1
)提交PUT请求,如果验证失败,它将使
编辑
操作就位,而不是重定向到
/companys/1/edit