Dynamic Paths in Helper

前端 未结 3 1098
生来不讨喜
生来不讨喜 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:13

    I would use a partial for this - instead of a helper. Wherever you want to display these links in your views, simply render the partial:

    <%= render :partial => "admin_links", :locals => { :model => model } %>
    

    In _admin_links.html.erb just paste the original code:

    <% if current_user %>
      <%= link_to "Edit", edit_model_path(model) %>
      <%= link_to "New", new_model_path %>
      <%= link_to "Delete", model, :confirm => "Your a Noob", :method => :delete %>
    <% end %>
    

提交回复
热议问题