Reputation: 758
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 %>
Upvotes: 0
Views: 116
Reputation: 11421
! Note, just an exemple, not a copy/paste ready code:
search page
here you'll need to check if object is in favorites list already
<div class="fav_toggle">
<%= link_to "Add to favorites", toggle_favorites_path(object, action: 'toggle'), remote: true %>
</div>
my favorites page
<div class="fav_toggle">
<%= link_to "Remove from favorites", toggle_favorites_path(object, action: 'remove'), remote: true %>
</div>
let's say toggle_favorites_path
in routes.rb
points to some_controller#fav_toggle
def fav_toggle
some logic here to check if fav exists or not and act as needed
add/remove favorite
end
fav_toggle.js.erb
<% if params[:action] == toggle %>
$('.fav_toggle').html('<%= link_to "Remove from favorites", add_to_favorites_path(object, action: 'toggle'), remote: true %>');
<% else %>
$('.fav_toggle').fadeOut().remove();
<% end %>
you'll need to work on this code to get it working, in the end you'll get what you expect.
Upvotes: 0
Reputation: 17631
What about the hidden field in your form ? Like so:
<%= hidden_field_tag :redirect_path, root_path %>
Then in your controller:
params[:redirect_path]
Upvotes: 1
Reputation: 8638
I understand, that you want to call favourite#destroy but want a switch in your controller to specify the redirect_to target.
One way is to submit additional information either as url parameter or as hidden field.
<%= 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| %>
<%= hidden_field_tag :return_to, 'your_target' %>
<%= f.submit "Remove from favorites %>
<% end %>
and in your controller
...
if params[:return_to]=='your_target'
redirect_to 'where_ever'
else
redirect_to users_path
end
Upvotes: 0