轨道:respond_with创建操作期间无法正常工作



这是我在控制器中的创建方法(旧方式):

  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语法

指定的"位置"仅在模型有效时使用。查看响应者文档 自定义选项是您需要的。还可以在此处检查答案 - 定义自定义响应器可以更清晰。

最新更新