I'm having trouble with if current_page? in a partial.
Section.html.erb
<div class="list" %>">
<%= render partial: 'items/item', collection: section.items, :as => :item %>
</div>
_item.html.erb
<% if current_page?(edit_polymorphic_path(@modulable)) %>
<span class="action">
<%= link_to %>
<%= link_to %>
</span>
<% end %>
The code works fine on a new page load and upon refreshing the page, but doesn't work on a dynamically added element with ajax.
Does this method work with Ajax? Am I doing something wrong? Or do I have to use another method?
I ended up using a different method. Instead of having the partial ask which page it's in, I passed a local variable to tell it what to do instead.
Section.html.erb
<div class="list" %>">
<%= render partial: 'items/item', collection: section.items, :as => :item, local: { edit: true } %>
</div>
_item.html.erb
<% if defined?(edit) && edit %>
<span class="action">
<%= link_to %>
<%= link_to %>
</span>
<% end %>
And I made sure I passed in the local variable to the new element in update.js.erb
items/update.js.erb
$('.list').append(
'<%= j (render partial: 'items/item', locals: { item: @item, edit: true }) %>'
);
It works, and is more explicit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.