Rails: How to POST internally to another controller action?

前端 未结 2 1427
南旧
南旧 2020-12-17 02:29

This is going to sound strange, but hear me out...I need to be able to make the equivalent of a POST request to one of my other controllers. The SimpleController

相关标签:
2条回答
  • 2020-12-17 02:49

    Inter-controller communication in a Rails app (or any web app following the same model-adapter-view pattern for that matter) is something you should actively avoid. When you are tempted to do so consider it a sign that you are fighting the patterns and framework your app is built on and that you are relying on logic has been implemented at the wrong layer of your application.

    As @ismaelga suggested in a comment; both controllers should invoke some common component to handle this shared behavior and keep your controllers "skinny". In Rails that's often a method on a model object, especially for the sort of creation behavior you seem to be worried about in this case.

    0 讨论(0)
  • 2020-12-17 02:57

    You shouldn't be doing this. Are you creating a model? Then having two class methods on the model would be much better. It also separates the code much better. Then you can use the methods not only in controllers but also background jobs (etc.) in the future.

    For example if you're creating a Person:

    class VerboseController < ApplicationController
      def create
        Person.verbose_create(params)
      end
    end
    
    class SimpleController < ApplicationController
      def create
        Person.simple_create(params)
      end
    end
    

    Then in the Person-model you could go like this:

    class Person
      def self.verbose_create(options)
        # ... do the creating stuff here
      end
    
      def self.simple_create(options)
        # Prepare the options as you were trying to do in the controller...
        prepared_options = options.merge(some: "option")
        # ... and pass them to the verbose_create method
        verbose_create(prepared_options)
      end
    end
    

    I hope this can help a little. :-)

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