其他父控制器 Rails 3.1 内的控制器的自定义验证错误
我的 Rails 应用程序中有一个 House 模型,它有很多交易。我正在房子的展示页面上显示这些优惠。当我提交表单时,如果使用redirect_to,一切正常;但是,如果交易模型存在验证错误,那么我的系统将无法正常工作。交易模型是否存在验证错误。
在我的routes.rb中,我
resources :houses do
resources :deals
end
在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
redirect_to工作正常,但我无法自定义我的验证表单模型失败时出现错误消息。
当 @deal.save 失败时,我检查了这个方法:
render :template => 'houses/show'
这个方法我在 模型验证失败时在 Rails 中的何处渲染注释控制器?
我只想渲染房子的 但不适合我,因为表单有一个操作:
/houses/name-of-house/deals
并且不重定向到 /houses/name-of-house/
如何从表单交易中获取错误验证(孩子),在我的动作表演中,来自房屋管理员?
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.
In my routes.rb I have
resources :houses do
resources :deals
end
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
With redirect_to working fine, but I can not custom my error message when fail the validation form model.
I have check this method when @deal.save fail:
render :template => 'houses/show'
This method I have seen in Where to render comments controller in Rails on model validations failure?
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
and not redirect to /houses/name-of-house/
How can I get the error validations from form deals (child), in my action show from house controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有同样的问题。我认为这是 Where to render 的重复项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.