其他父控制器 rails 3.1 中的自定义验证错误表单控制器

编程入门 行业动态 更新时间:2024-10-27 12:38:28
本文介绍了其他父控制器 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 中的自定义验证错误表单控制器

本文发布于:2023-10-26 10:54:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   自定义   表单   错误   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!