2016-11-13 28 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