I have a helper method that creates a link to follow an entity. This is based on this answer. Here's the code:
def link_to_follow(thing, extra_css_classes="")
if current_user.following?(thing)
link_to(
"Unfollow",
send("unfollow_#{thing.class.name.underscore.pluralize}_path", id: thing.id),
method: :post,
id: "unfollow_link_#{thing.id}",
class: "btn btn-outline-primary btn-block #{extra_css_classes}",
remote: true)
else
link_to(
"Follow",
send("follow_#{thing.class.name.underscore.pluralize}_path",id: thing.id),
method: :post,
id: "follow_link_#{thing.id}",
class: "btn btn-primary btn-block #{extra_css_classes}",
remote: true)
end
end
This calls the follow/unfollow controller remotely, makes the user follow/unfollow the entity, and then replaces the existing button with javascript:
$("#follow_link_<%=instance.id%>").replaceWith("<%=j link_to_follow(instance)%>");
My problem is that when old button is being replaced with the new one, the extra_css_classes
are not passed to the new link_to_follow
and therefore it ends up looking differently.
Is there a way to get the css classes from the old button and pass them to the link_to_follow
helper?