I have recently started breaking up some of my apps using engines I ran into some issues with the routing from some links in a shared partial I am using. The partial looks like this:
shared/delete_link.html.erb
<%= link_to object,
method: :delete,
data: { confirm: 'Are you sure?' } do%>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
I call the partial like this; passing it the object I want deleted:
<%= render "shared/delete_link", :object => search %>
This works fine for models/routes that are still in the main app but now I have a situation like this:
|-controller(main-app)
|--show(main-app)
|---partial(engine)
|----partial-delete-link(main-app)
|-----link-route(engine)
Where the partial is rendered by a controller in the main app with a delete link for a route in the engine. The only thing that matters(I think) is that the controller is in the main app and the route for the object is in the mounted engine so it needs to be namespaced.
How can I restore functionality without putting a lot of logic into my view?
Don't overdo it when refactoring.
Using a partial to create a single element is hardly optimal. Instead you can just create a helper method:
module LinksHelper
def link_to_destoy(object, **kwargs)
opts = {
method: :delete,
data: { confirm: 'Are you sure?' }
}.deep_merge(kwargs)
link_to object, opts do
content_tag :span, class: 'glyphicon glyphicon-remove'
end
end
end
By using a method instead you can easily create a wrapper that proxies to another method while filling in the default args. In comparision partials are good for large chunks of html but still messy when it comes to variable passing and composition.
To create a link to a namespaced path in rails you pass an array:
# /foos/bars/:id
<%= link_to_destoy([:foo, bar]) %>
# /foos/:foo_id/bars/:id
<%= link_to_destoy([foo, bar]) %>