On a search results page I have "Add to favorites" and "Remove from favorites" button partials that use respond_to and format.js so the page does not refresh, but the page toggles between displaying the "Add to favorites" and "Remove from favorites" button partials. That works fine.
But on the user's "My Favorites" page and on their profile page, if the user clicks "Remove from favorites" button, I do want to refresh the page, so that removed favorite no longer displays.
I can control when to refresh page (respond_to format.html) and when to toggle the buttons (respond_to format.js), by passing local variable to partial used for remote: true (remote: @refresh_page), but in the favorites controller, the format.html is set to redirect_to users_path.
So how can I send a variable from the partial to the controller destroy action method telling it where to redirect to?
Here is the partial:
<%= form_for(current_user.favorites.find_by_followed_event_id(user_event),
html: { id: "event_number_#{user_event.id}", method: :delete }, remote: @refresh_page) do |f| %>
<%= f.submit "Remove from favorites %>
<% end %>
form
to add/remove to/from favorites? alink_to
an action would be enough I think, in that link you'll useremote: true
for ajax, and send whatever params you need but when you do it with ajax you won't need a redirect, just add proper js in theaction_name.js.erb
file to replace one partial with another. – rmagnum2002link_to
and inaction.js.erb
you can checkif
thatparam
is present, than just remove the favorited element,else
toggle butons. – rmagnum2002