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

后端 未结 4 639
萌比男神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:09

    First, as other have suggested, create a catch-all route at the bottom of routes.rb:

    map.connect ':name', :controller => 'aliases', :action => 'show'
    

    Then, in AliasesController, you can use render_component to render the aliased action:

    class AliasesController < ApplicationController
      def show
        if alias = Alias.find_by_name(params[:name])
          render_component(:controller => alias.page_type.controller, 
                            :action => alias.page_type.action,
                            :navigation_node_id => alias.navigation_node.id)
        else
          render :file => "#{RAILS_ROOT}/public/404.html", :status => :not_found
        end
      end
    end
    

提交回复
热议问题