我的 Rails 应用程序中有一个 House 模型,它有很多交易。我在房子的展示页面上显示这些交易。当我提交表单时,如果使用redirect_to,一切正常;但是,如果 Deal 模型存在验证错误,则说明我的系统无法正常工作。如果 Deal 模型存在验证错误。
在我的routes.rb我有
resources :houses do
resources :deals
end
在deal_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/
如何在房屋控制器的动作秀中从表单交易(子)中获取错误验证?