Question

I want to constrain my routes if a match is found in the params like so:

constraints :client_code => User.all.client_code.any? do
  get ":client_code" => "share#index", :as => :shares
  get ":client_code/:id" => "share#show", :as => :share
end

What should go instead of User.all.client_code.any?? I really cannot figure it out.

Thanks! UPDATE

I've made a constraints object like so:

class ClientCodeConstraint
  def initialize
    @users = User.all
  end

  def matches?(request)
    @users.where(:client_code => request).first
  end
end

And changed the constraints ... line in routes.rb to this:

 constraints ClientCodeConstraint.new do ...

Am I on the right track? I'm getting this response:

undefined method `where' for #<Array:...
Was it helpful?

Solution

Solved with this method, don't know how efficient it is though:

def matches?(request)
  @users.each do |user|
    return true if request.path_parameters[:client_code] == user.client_code
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top