Question

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.

Était-ce utile?

La solution

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} %>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top