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?
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.
来源:https://stackoverflow.com/questions/7838227/custom-validations-errors-form-controller-inside-other-parent-controller-rails-3