I have a event search results page where users can click a "Add to favorites" button.
Clicking the button performs a POST to add the event to the user's favorites. If they search again, they will now see a "Remove from favorites" button instead of an "Add to favorites" button for that event.
Currently in the favorites controller create action I use a respond_to so format.js allows user to stay on that search results page.
But how do I then render the partial for the "Remove from favorites" button, instead of the partial for the "Add to favorites" button.
That way they can click "Remove from favorites" for the item they just favorite if necessary.
Screenshot and code below. Thanks in advance.
-------------- search results item renderer ---------------
<div id="add_remove_favorite" class="user_event_info_container">
<% if (signed_in? && current_user && current_user.id != user_event.user_id) %>
<%= render partial: "shared/add_remove_favorite",
locals: { user_event: user_event } %>
<% end %>
</div>
------------- shared/_add_remove_favorite.html.erb ---------
<% if current_user.following?(user_event) %>
<%= render partial: 'shared/remove_favorite',
locals: { user_event: user_event } %>
<% else %>
<%= render partial: 'shared/add_favorite',
locals: { user_event: user_event } %>
<% end %>
-------------- shared/_add_favorite.html.erb ----------
<%= form_for(current_user.favorites.build(followed_event_id: user_event.id), remote: true) do |f| %>
<div class="hidden"><%= f.hidden_field :followed_event_id %></div>
<%= f.submit "Add to favorites",
class: "info_button_small user_event_summary_item" %>
<% end %>
-------------- shared/_remove_favorite.html.erb ------------
<%= form_for(current_user.favorites.find_by_followed_event_id(user_event),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Remove from favorites", class: "info_inline_control info_button_small user_event_summary_item" %>
<% end %>
-------------- views/create.js.erb ------------ What should go here? I assume this is where I perform the magic.
-------------- views/destroy.js.erb ------------ What should go here? I assume this is where I perform the magic.