I have a controller:
controller/streets_controller.rb
class StreetsController < ApplicationController
before_action :set_street, only: [:show, :edit, :update, :destroy]
def show
@house = @street.houses.build
end
.......
And a view for that show
action. There I also display a form to create new street houses:
views/streets/show.html.erb
<h1>Street</h1>
<p>@street.name</p>
<%= render '/houses/form', house: @house %>
When somebody submits the houses/form
the request goes to the houses_controller.rb
class HousesController < ApplicationController
def create
@house = House.new(house_params)
respond_to do |format|
if @house.save
format.html { redirect_back(fallback_location: streets_path) }
format.json { render :show, status: :created, location: @house }
else
format.html { redirect_back(fallback_location: streets_path) }
format.json { render json: @house.errors, status: :unprocessable_entity }
end
end
end
So far it works that when somebody inserts correct house_params
it redirects back and creates the house correctly
But when somebody inserts wrong house_params
it redirects_back but it doesn't show the house errors in the form, it shows the form for a new house @house = @street.houses.build
How can I redirect to the StreetsController
with the @house
object, so that the errors are shown and also the house form is filled?
Thanks
Aucun commentaire:
Enregistrer un commentaire