Rails ajax fav button for user messages

I requested user messages (embedded resources, obviously) and had a long list of messages from different users. I would like the user to be able to click the little star next to each post in order to love this particular post via ajax. Any suggestions on how to achieve this? The problem I am facing is a few favorite buttons on the same page, which allows you to use multiple messages.

This is similar to what Gmail does with your favorite messages in your inbox. Actually, this is exactly so.

+7
source share
2 answers

First you need to set up a database to handle this, I personally would go with has_many: through the association, because it provides more flexibility over has_and_belongs_to_many. The choice, however, is up to you. I recommend that you look for different types of APIs and decide for yourself. This example will deal with has_many: through.

Models

# user.rb (model) has_many :favorites has_many :posts, :through => :favorites # post.rb (model) has_many :favorites has_many :users, :through => :favorites # favorite.rb (model) belongs_to :user belongs_to :post 

controller

 # favorites_controller.rb def create current_user.favorites.create(:post_id => params[:post_id]) render :layout => false end 

Routes

 match "favorites/:post_id" => "favorites#create", :as => :favorite 

JQuery

 $(".favorite").click(function() { var post_id = $(this).attr('id'); $.ajax({ type: "POST", url: 'favorites/' + post_id, success: function() { // change image or something } }) }) 

Notes

This suggests a couple of things: using Rails 3, using jQuery, every favorite icon has an html identifier with a message id. Keep in mind that I have not tested the code and I wrote it in this window, so you probably have to fix some minor issues, but this should give you an idea of ​​how I usually do it. Visual material and the like I will leave you.

If anyone finds any errors, feel free to edit this post.

+10
source

Make form_tag for each favorite button with :remote => true . Add the identifiers of the message and the current user as hidden fields for each of the forms.

To update the status of favorites, you can write an RJS view that will contain jQuery to update your favorite so that the post is elected.

Something like that:

  - for post in @posts = form_tag toggle_favorite_post_path, :remote => true = hidden_field_tag :post_id, post.id = hidden_field_tag :user_id, current_user.id - if Favorite.where(:post_id => post.id, :user_id => current_user.id).exists? # TODO: move this to model = submit_tag "Add to favorites", :class => "favorite-post-button" - else = submit_tag "Remove from favorites", :class => "unfavorite-post-button" 

You can customize the appearance of the submit buttons (s) to be a star image with CSS.

If you want to provide the ability to select multiple messages, and then all of them at once, you will have to write your own JavaScript code to process it. In the click handler for the “Favorites all selected” button, then inside this handler, collect all the selected message identifiers, serialize the identifiers into a string and send them back to the controller.

+1
source

All Articles