1

Is there a way to determine within the padrino controller what the route name is? For example, in a before block, I'd like to be able to take some predetermined action based on the route. So, something like this:

before do
  check_authorization_for(:route_name)
end

Where ":route_name" would be something like "update" or "delete" or "create" or whatever. As it is now, I just have the code to check whether or not the current user has permission to perform the requested action at the beginning of each controller method. Our authorization scheme relies on information in our DB, and these permissions are keyed on the controller action, so if there's a way to determine the name of the route in the controller (i.e. if I can determine that the "update" action is being called), I can just have a single block to test for authorization instead of having to do the check in each action.

I feel certain there must be a very simple way to do this, but I can't seem to find any documentation on it. Thanks in advance for the assistance.

1 Answers1

4

Try

before :show, :edit, :destroy do
    Post.find(params[:id])
end

Or if you prefer access directly to the route object:

request.route_obj

I.e.

request.route_obj.controller

If you want to mix current path, there is something similar to current_page in rails.

# http://localhost:3000/category/1/products/page/3
current_path('page' => 4) # => http://localhost:3000/category/1/products/page/4
DAddYE
  • 1,719
  • 11
  • 16
  • Unfortunately that's not really what I'm looking for. In our DB, we'll have an authorization record similar to {resource: 'foo', action: 'update', acl_id: 1234}, so in the controller, I'd like to be able to do something like "Actions.where(resource: 'foo', action: 'update')" in order to find the "acl_id" value. In this example, "update" is the controller action being called, so it wouldn't be in an "id" param, it would be the actual controller action. Does that make sense? – orderedchaos Jun 20 '12 at 13:51
  • That was almost exactly what I needed. I ended up using request.route_obj.named.to_s, which got me what I was looking for. I had never seen or heard of request.route_obj, so that was incredibly helpful. Thanks so much. – orderedchaos Jun 21 '12 at 14:29