Rails多态重定向嵌套资源

时间:2016-01-27 22:29:36

标签: ruby-on-rails polymorphic-associations

将嵌套资源和多态关联归结为重定向问题。我想我可以找到前进的方向,但我想找出可以被认为是最佳实践的方法。

我有以下嵌套资源:

  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

注释是多态关联(我稍后也会使用工具模型)。

我的大多数代码都受到了启发,但出色的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

请注意 *如何参考BOK * 部分。这就是我的问题所在。一旦我更新&#34;注意&#34;我想重定向到@noteable相关的控制器(这里是Processus)但是为了构建URL,我需要有一个@bok对象,在这个例子中我没有,因为我实际上并不需要它。

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

我的重定向网址应为http://localhost:3000/navigate/boks/1/processus/2,但为了构建它,我需要一个Bok对象,我无法进入上面的控制器(因为我不需要它)。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

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

相关问题