0

組織の新しいイベントを作成するためのフォームがあります。ルーティングは次のとおりです。

resource :organisations do
  resource :events
end

イベントを編集すると、成功すると、次のようにイベント コントローラーの show アクションに戻ります。

def update
    @organisation = current_user.organisations.find(params[:organisation_id])
    @event = @organisation.events.find(params[:id])
    if @event.update_attributes(params[:event])
        # Handle a successful update.
        flash[:success] = "Event updated"
        redirect_to organisation_event_path
    else
        render 'edit'
    end
end

イベントを作成するときに、次のように実装された events show アクションにリダイレクトすることも必要です。

def create
    @organisation = current_user.organisations.find(params[:organisation_id])
    @event = @organisation.events.create(params[:event])
    if @event.save
        flash[:success] = "Event added!"
        redirect_to organisation_event_path
    else
        render 'new'
    end
end

ただし、これにより次のエラーが発生します。No route matches {:action=>"show", :controller=>"events"}

これは、私が把握できる限り、URI にイベント ID/名前が含まれていないためです。おそらく、パス生成に関する私の理解不足ですが、どうすれば目的の結果を得ることができますか?

4

1 に答える 1

0

最初のリダイレクトが機能した理由はわかりませんが、組織とイベントの 2 つのリソースの ID からパスを作成しているため、organisation_event_path には 2 つの引数が必要です。したがって、次のように呼び出す必要があります。

organisation_event_path(@organization, @event)
于 2012-05-14T07:26:54.400 に答える