Rails 多态重定向嵌套资源

Rails Polymorphic Redirect Nested Resources

遇到嵌套资源和多态关联的重定向问题。我想我可以找到前进的方向,但是我想找出什么可以被视为最佳实践。

我有以下嵌套资源:

  namespace :navigate do
    resources :boks, :only => [:show] do
      resources :groups,  :only => [:show]
      resources :categories, :only => [:show]
      resources :tools, :only => [:show, :index]
      resources :artifact_types, :only => [:show]
      resources :artifacts, :only => [:show, :index]
      resources :processus do
        resources :notes, module: :processus
      end
    end
  end

Notes 是一个多态关联(我稍后也会将其与 Tool 模型一起使用)。

我的大部分代码都受到启发,但出色的 gorails.com 插曲:https://gorails.com/episodes/comments-with-polymorphic-associations

我用来管理笔记的控制器是:

class Navigate::NotesController < Navigate::NavigateController

  before_action :authenticate_user!

  def create
    @note = @noteable.notes.new note_params
    @note.user = current_user
    @note.save
    redirect_to [:navigate, @bok, @noteable], notice: "Your note was succesfully created."
  end

  def update
    @note = @noteable.notes.where(user: current_user).first
    if @note.update(note_params)
      redirect_to polymorphic_url([:navigate,*** HOW TO REFERENCE BOK ***, @noteable]), notice: "Your note has been updated."

    else
      flash.now[:alert] = "Unable to update your note."
      render :edit
    end
  end

  private
    def note_params
      params.require(:note).permit(:content, :public)
    end
end

注意 * 如何参考书籍 * 部分。这就是我的问题所在。更新 "Note" 后,我想重定向到 @noteable 相关控制器(这里是 Processus),但是为了构建 URL 我需要一个 @bok 对象,我在这个实例中没有因为我其实不需要它。

我也可以检索到正确的@bok 模型,但我想知道是否有其他方法可以处理此重定向?

我用于重定向的 URL 应该是 http://localhost:3000/navigate/boks/1/processus/2 但是为了构建它我需要一个 Bok 对象,我没有在上面的控制器中使用它(因为我不需要它).

有什么想法吗?谢谢!

如果没有嵌套对象的 ID,则无法引用嵌套对象的路由。