我的 Rails 应用程序中有一个 House 模型,它有很多交易。我正在房子的展示页面上显示这些优惠。当我提交表单时,如果使用redirect_to,一切正常;但是,如果交易模型存在验证错误,那么我的系统将无法正常工作。交易模型是否存在验证错误。
In my 路线.rb I have
resources :houses do
resources :deals
end
In Deals_controller.rb我有下一个方法创建:
def create
@house = House.find_by_slug(params[:house_id])
@deal = @house.deals.build(params[:deal])
@deal.user = current_user
respond_to do |format|
if @deal.save
format.html { redirect_to @house, :notice => 'Your offer has been created successfully' }
format.json { render json: @house, status: :created, location: @house }
else
format.html { redirect_to @house, :alert => 'Oops, something went wrong. Please try again' }
format.json { render json: @house.errors, status: :unprocessable_entity }
end
end
end
With 重定向到工作正常,但当验证表单模型失败时,我无法自定义错误消息。
当 @deal.save 失败时我检查了这个方法:
render :template => 'houses/show'
这个方法我见过当模型验证失败时,Rails 中的注释控制器在哪里呈现? https://stackoverflow.com/questions/5175385/where-to-render-comments-controller-in-rails-on-model-validations-failure
我只是想渲染房子的但不适合我,因为表格有一个动作:
/房屋/房屋名称/交易
并且不重定向到/房屋/房屋名称/
在我的动作表演中,如何从内部控制器的表单交易(子)中获取错误验证?