What's the correct way to run one controller action from another controller action without an HTTP redirect?

后端 未结 6 602
无人及你
无人及你 2020-12-30 09:33

I\'d like to be able to dispatch from one controller action to another conditionally, based on a combination of query parameters and data in the database.

What I hav

6条回答
  •  一向
    一向 (楼主)
    2020-12-30 10:08

    Instead of calling code across actions, extract the code to lib/ or something, and call that code from both controllers.

    # lib/foo.rb
    module Foo
      def self.bar
      # ...
      end
    end
    
    # posts_controller
    def index
      Foo.bar
    end
    
    # things_controller
    def index
      Foo.bar
    end
    

提交回复
热议问题