Dynamic Paths in Helper

前端 未结 3 1099
生来不讨喜
生来不讨喜 2021-01-31 17:31

I\'m trying to create a helper method for my admin links. In quite a few views I have the code

<% if current_user %>
<%= link_to \"Edit\", edit_model_pa         


        
3条回答
  •  粉色の甜心
    2021-01-31 18:05

    basically you need to transform the class name of the model into something pointing to the correct path.

    model_name = m.class.to_s.underscore
    

    And then use it to call the appropriate path methods

    link_to "edit", send("edit_#{model_name}_path", m)
    

    As an aside, you don't need to put the link_tos in #{} because that function simply returns a string.

提交回复
热议问题