自定义约束匹配?'方法从未调用

时间:2015-07-28 08:42:31

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

我试图仅通过Ajax限制某些路由, 并写下routes.rblib/constraints/only_ajax_request.rb,如下所示。

的routes.rb

resources :orders, except: [:destroy] do
  collection do
    post 'login_or_sign_up', to: 'orders#login_or_sign_up', constraints: OnlyAjaxRequest.new
  end
end

LIB /约束/ only_ajax_request.rb

class OnlyAjaxRequest
  def matches?(request)
    request.xhr?
  end
end

但是,这个规范失败了。

规格/控制器/ orders_controllers_spec.rb

  describe 'POST #login_or_sign_up' do
    let(:user) { create(:login_user) }

    it 'only ajax' do
      post :login_or_sign_up, user: { email: user.email,
                                      password: user.password }
      expect(response.status).to eq(404)
    end
  end

我将断点设置为OnlyAjaxRequest,

Class OnlyAjaxRequest
  def matches?(request)
    binding.pry
    request.xhr?
  end
end

但从未打过电话。

如何设置约束以发布路由?

0 个答案:

没有答案