2016-11-13 37 views
0

我有一个控制器:重定向回来的错误

控制器/ streets_controller.rb

class StreetsController < ApplicationController 
    before_action :set_street, only: [:show, :edit, :update, :destroy] 

    def show 
    @house = @street.houses.build 
    end 

    ....... 

而对于show作用的图。在那里,我也表现形式,创造新街的房屋:

的意见/街道/ show.html.erb

<h1>Street</h1> 
<p>@street.name</p> 

<%= render '/houses/form', house: @house %> 

当有人提交houses/form请求转到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 

到目前为止,它的工作原理是,当有人插入正确house_params它重定向回,并创建房子正确

但是,当有人插入错误house_params它redirects_back但它并不显示在表格中房子的错误,它显示了新房子@house = @street.houses.build

我怎样才能重定向到StreetsController@house形式对象,以便显示错误并填充房屋形式?

谢谢

+0

用户可能重定向到的可能页面是什么? –

回答

1

通常,当有一个错误在创建行动,你将会使“新”的观点,而不是重定向回来。在你的情况下,你可以尝试重定向到街道显示路径,并传递@house属性作为查询参数。然后在StreetsController#show中,将house params作为参数传递给build