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 %>
formto add/remove to/from favorites? alink_toan action would be enough I think, in that link you'll useremote: truefor 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.erbfile to replace one partial with another. - rmagnum2002link_toand inaction.js.erbyou can checkifthatparamis present, than just remove the favorited element,elsetoggle butons. - rmagnum2002