1

これが私のコントローラーの作成方法です(古い方法):

  def create
    @athlete = Athlete.find(params[:video][:athlete_id])
    @video = Video.new(params[:video])

    if @athlete.can_add_another_video? && @video.save
      flash[:notice] = "Successfully created"
      PandaWorker.perform_async(@video.id)
      log_activity(@video.logging_information)
    else
      flash[:notice] = @video.errors.full_messages.to_sentence
    end

    respond_with @video, location: athlete_profile_showcase_path
  end

新しい方法:

  def create
    @athlete = Athlete.find(params[:video][:athlete_id])
    @video = Video.new(params[:video])

    if @athlete.can_add_another_video? && @video.save
      flash[:notice] = "Successfully created"
      PandaWorker.perform_async(@video.id)
      log_activity(@video.logging_information)
      respond_with @video, location: athlete_profile_showcase_path
    else
      flash[:notice] = @video.errors.full_messages.to_sentence
      redirect_to athlete_profile_showcase_path
    end
  end

上記のコードの最初の部分は、ビデオ オブジェクトの保存が行われない場合に失敗します。VideosController#new指定した場所をたどるのではなく、メソッドにリダイレクトしようとします。明らかに最初の方法は間違っていますが、その理由はわかりません。どんな助けでも大歓迎です!respond_withまだ構文を完全に理解しようとしています

4

1 に答える 1

1

モデルが有効な場合にのみ使用される指定された「場所」。レスポンダー ドキュメントカスタム オプションをご覧ください。ここでも回答を確認してください- カスタムレスポンダーを定義すると、はるかにきれいになります。

于 2013-10-06T21:55:09.420 に答える