In my app, users have the ability to favorite items (“listings”). To make it a smooth user experience, I’m handling the favoriting through ajax. The only problem is that the ajax isn’t rendering correctly.
The html.erb related to the favorite button is the following:
<div class="favorite-btn"><%= link_to "Save to Favorites", favorites_path(id: listing.id), method: :post, remote: true, class: "btn btn-danger btn-xs" %></div>
I created a create.js.erb with the following and similar code in a destory.js.erb that replaces the “Remove as Favorite” with “Save to Favorites”:
Any thoughts on where I’m going wrong with this?
@listing and then in the
link_to you use a local variable,
listing. I’m guessing you want the instance variable.
Next, instead of
listing_path(:id=> listing.id) please try
Finally, this isn’t your problem, but there is a view helper you can use that will output the
listing_<%= @listing.id %> for you. it is
dom_id you can read more about it here. So using this method you would do
$("#<%= dom_id(@listing) %> .favorite-btn")
I hope that helps, let me know how it works out and if you have any further trouble with this.
Ok awesome. I completely forgot about dom_id.
The reason that I was using
@listing in one place and just
listing in another is that the favorite_button html was extracted into a view partial (
render "favorite_button", listing: @listing).
I’m a little hung up on modifying the params. It works fine for the destroy method because the natural Rails route for a delete action is /favorites/:id(.:format), which expects an id to be passed in the params. With the create action, the natural Rails route is /favorites(.:format). As a result, I was passing the id as I was as
Maybe this has to do with how I setup the models/controllers? In the create action,
@listing = Listing.find(params[:id])
if current_user.user_favorite_listings << @listing
It looks like Chad’s suggestion on modifying the delete path was the right solution and it now works as expected.
My only remaining question on this is whether the way I am passing the id params through the create action is the proper way to handle this case. Thanks everyone!
@realDLee I overlooked what you were actually creating, which was a favorite.
The favorites route should be nested under listing.
resources :listings do
This will give you a route like this:
Then, in the favorites controller you will get
params[:listing_id] as the id of the listing that the user is creating the favorite for.