Primero necesitas configurar la base de datos para manejar esto, personalmente iría con una asociación has_many: through porque proporciona más flexibilidad sobre has_and_belongs_to_many. La elección, sin embargo, depende de usted. Te recomiendo que busques los diferentes tipos en la API y lo decidas por ti mismo. Este ejemplo tratará con has_many: through.
Modelos
# 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
Controlador
# favorites_controller.rb
def create
current_user.favorites.create(:post_id => params[:post_id])
render :layout => false
end
Rutas
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
}
})
})
Notas
Esto supone un par de cosas: Utilización de los carriles 3, utilizando jQuery, cada icono favorito tiene un id html con el ID del anuncio. Tenga en cuenta que no he probado el código y lo escribí en esta ventana, por lo que probablemente tenga que solucionar algunos problemas menores, pero debería darle una impresión de cómo I generalmente lo hace. Lo visual y tal lo dejo a usted.
Si alguien detectar cualquier error no dude en modificar este post.