Frage

I have a dozen different Views/Controllers for administrator. In every view I have got something like this:

<%= link_to edit_topic_path(topic) do %>
     <span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to topic, method: :delete, confirm: "Are you sure?" do %>
     <span class="glyphicon glyphicon-trash"></span>
<% end %>

Now my question is, How can I DRY this so I just pass some vars or object to a method and generate the above code for different views.

Thank you.

War es hilfreich?

Lösung

You should crate a partial

_controls.html.erb

<%= link_to path do %>
     <span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to model, method: :delete, confirm: "Are you sure?" do %>
     <span class="glyphicon glyphicon-trash"></span>
<% end %>

Then you can call it as follows:

<%= render :partial => "controls", :locals => {:path => edit_topic_path(topic), :model => topic} %>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top