当路由嵌套在rails中时,跳过控制器中的before_action

时间:2014-08-11 15:34:18

标签: ruby-on-rails ruby ruby-on-rails-4

我有这样的路线。我已经使用流嵌套批次路由。

resources :batches

  resources :streams do
    resources :batches do
      collection do
        post 'create_batch'
      end
    end
  end

当我转到链接时。

create_batch_stream_batches_path(stream)

在bacthesControlller.rb中,它调用before action:set_batch,它不应该作为before_action

before_action :set_batch, only: [:show, :edit, :update, :destroy]
  def create_batch
    logger.info params
    binding.pry

  end

我尝试使用skip_before_action create_batch,但它不起作用。

1 个答案:

答案 0 :(得分:0)

在嵌套路由中,您只需传递一个自定义参数,该参数将在请求期间传递给控制器​​:

resources :batches
  resources :streams do
    resources :batches, skip: true do
    end
  end
end

在控制器中检查参数是否通过,如果设置了标志,则不运行挂钩:

before_action :set_batch, unless: -> { params[:skip] }