Best practices to handle routes for STI subclasses in rails

后端 未结 18 1308
面向向阳花
面向向阳花 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 17:11

    I had the same problem. After using STI, the form_for method was posting to the wrong child url.

    NoMethodError (undefined method `building_url' for
    

    I ended up adding in the extra routes for the child classes and pointing them to the same controllers

     resources :structures
     resources :buildings, :controller => 'structures'
     resources :bridges, :controller => 'structures'
    

    Additionally:

    <% form_for(@structure, :as => :structure) do |f| %>
    

    in this case structure is actually a building (child class)

    It seems to work for me after doing a submit with form_for.

提交回复
热议问题