Best practices to handle routes for STI subclasses in rails

后端 未结 18 1262
面向向阳花
面向向阳花 2020-11-30 16:25

My Rails views and controllers are littered with redirect_to, link_to, and form_for method calls. Sometimes link_to and <

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 16:49

    Following the idea of @Prathan Thananart but trying to not destroy nothing. (since there is so much magic involved)

    class Person < Contact
      model_name.class_eval do
        def route_key
         "contacts"
        end
        def singular_route_key
          superclass.model_name.singular_route_key
        end
      end
    end
    

    Now url_for @person will map to contact_path as expected.

提交回复
热议问题