1

Railsアプリでいくつかの簡単な関連付けをセットアップしました:

resources :properties do
 resources :units
end

ユニットを更新した後:

「localhost:3000/properties/2/units/3/edit」

適切な場所にリダイレクトされるようにしたい。

「更新」をクリックすると、次の場所にリダイレクトされます。

「localhost:3000/units/3」ですが、「localhost:3000/properties/2/units/3/」に移動する必要があります

私のユニットコントローラーには、次のものがあります。

if @unit.update_attributes(params[:unit])
    format.html { redirect_to(property_units_path(@property, @unit), :notice => 'Unit was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @unit.errors, :status => :unprocessable_entity }
  end

これは私の redirect_to 関数で適切に使用されていますか? 私はまだルートの仕組みに慣れようとしていますが、これでうまくいくと思いますか?

ありがとう!

4

1 に答える 1

0

あなたはほとんどそこにいます。次の場所にリダイレクトする必要があります。

format.html { redirect_to(property_unit_path(@property, @unit), :notice => 'Unit was successfully updated.') }

これを機能させるには、単一バージョンのルーティング ヘルパー メソッドを使用する必要があることに注意してください。詳細については、Rails ガイドのルーティングを確認してください。

于 2012-06-26T19:15:36.477 に答える