其他父控制器 rails 3.1 中的自定义验证错误表单控制器 [英] custom validations errors form controller inside other parent controller rails 3.1
问题描述
我的 rails 应用中有一个 House 模型,它有很多交易.我在房子的展示页面上显示这些交易.当我提交表单时,如果使用 redirect_to,一切正常;但是,如果 Deal 模型存在验证错误,那么我的系统就无法正常工作.如果 Deal 模型存在验证错误.
I have a House model in my rails app that has_many deals. I am displaying these deals on the house's show page. When I submit the form everything works fine if is with redirect_to; however, if there are validation errors on the Deal model, then my system not working fine. If there are validation errors on the Deal model.
在我的routes.rb我有
resources :houses do
resources :deals
end
在 deals_controller.rb 中,我创建了下一个方法:
In deals_controller.rb I have the next method create:
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
redirect_to 工作正常,但是当验证表单模型失败时我无法自定义我的错误消息.
With redirect_to working fine, but I can not custom my error message when fail the validation form model.
@deal.save 失败时我检查了这个方法:
I have check this method when @deal.save fail:
render :template => 'houses/show'
这个方法我在哪里见过在模型验证失败时在 Rails 中呈现评论控制器?
我只想渲染房子的但不适合我,因为表单有一个动作:
I would simply like to render the house's But not working for me, because the form have a action to:
/houses/name-of-house/deals
而不是重定向到/houses/name-of-house/
如何在我的动作表演中从房屋控制器获得表单交易(子)的错误验证?
推荐答案
我有同样的问题.我认为这是 在哪里渲染的副本模型验证失败时 Rails 中的评论控制器? - 我在那里添加我自己的响应.
I have the same problem. I think this is a duplicate of Where to render comments controller in Rails on model validations failure? - I am adding my own response there instead.
这篇关于其他父控制器 rails 3.1 中的自定义验证错误表单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!