How to get request's target controller and action with Rails 3?

后端 未结 4 752
情深已故
情深已故 2021-02-01 01:35

In the application controller before filter.

class ApplicationController < ActionController::Base
  before_filter :authenticate

  def authenticate
    # How          


        
相关标签:
4条回答
  • 2021-02-01 02:12

    In Rails 3.2 you no longer need to call controller.action_name explicitly instead just "action_name".

    before_filter :check_if_locked
    
    
    def check_if_locked
      puts action_name
      puts controller_name
    end
    
    0 讨论(0)
  • 2021-02-01 02:19
    request.parameters['controller']
    request.parameters['action']
    
    0 讨论(0)
  • 2021-02-01 02:21
    class ApplicationController < ActionController::Base
      before_filter :authenticate
    
      def authenticate
        # How do we know which controller and action was targetted?
        params[:controller]
        params[:action]
        # OR
        controller.controller_name
        controller.action_name    
      end
    end
    
    0 讨论(0)
  • 2021-02-01 02:24

    You can get full url object using

    url = Rails.application.routes.recognize_path(request.env['PATH_INFO'])

    now you can get components as

    url[:controller]

    url[:action]

    By default you can also use params[:controller] and params[:action] respectively during request/response life cycle.

    0 讨论(0)
提交回复
热议问题