Is it a bad idea to reload routes dynamically in Rails?

后端 未结 4 650
萌比男神i
萌比男神i 2021-02-03 13:43

I have an application I\'m writing where I\'m allowing the administrators to add aliases for pages, categories, etc, and I would like to use a different controller/action depend

4条回答
  •  有刺的猬
    2021-02-03 14:19

    I'm not sure I fully understand the question, but you could use method_missing in your controllers and then lookup the alias, maybe like this:

    class MyController
      def method_missing(sym, *args)
        aliased = Alias.find_by_action_name(sym)
        # sanity check here in case no alias
    
        self.send( aliased.real_action_name )
        # sanity check here in case the real action calls a different render explicitly
        render :action => aliased.real_action_name
      end
    
      def normal_action
        @thing = Things.find(params[:id])
      end
    end
    

    If you wanted to optimize that, you could put a define_method in the method_missing, so it would only be 'missing' on the first invocation, and would be a normal method from then on.

提交回复
热议问题