I solve the issue
On Aug 16, 2015 8:56 AM, "Elizabeth McGurty" <[email protected]> wrote:

> Nilay,
>
> What's the status of your effort here?
>
> Liz
>
> On Monday, August 10, 2015 at 3:36:52 PM UTC-4, nilay singh wrote:
>>
>> I am trying to create a like and dislike function inside rails for that I
>> am using lin_to helper to pass params but there is an issue when ever
>> someone tries to copy paste the links it updated the database . I am using
>> ajax to make this function work here is the code for my method .
>>
>> Controller code :
>>
>>  class FeedLikesController < ApplicationController
>>             before_action :authenticate_user! ,only: [:create, :destroy]
>>              before_action :get_feed ,only: [:create, :destroy]
>>
>>
>>               def index
>>                 @fees = FeedLike.all
>>                 respond_to do |format|
>>
>>                         format.html
>>                         format.js
>>
>>
>>              end
>>               end
>>              def update
>>                 @feed_likes = FeedLike.find_or_create_by(feed_like_params)
>>                     respond_to do |format|
>>                         if @feed_likes.save
>>                         format.html { redirect_to root_url, notice: 'Like ' }
>>
>>                         else
>>
>>                         end
>>                    end
>>             end
>>               def create
>>                     @feed_like_counter= Feed.find(params[:feed_id])
>>                     @feed_likes = FeedLike.find_or_create_by(:feed_id => 
>> params[:feed_id],:user_id =>params[:user_id])
>>                      @f = @feed_like_counter.like_count
>>                      @feed_like_counter.like_count = @f+1
>>                      @feed_like_counter.save
>>                     respond_to do |format|
>>                         if @feed_likes.save
>>                         format.html { redirect_to root_url, notice: 'Like ' }
>>                         format.js
>>                         else
>>
>>                         end
>>                    end
>>               end
>>
>>
>>               def delete
>>
>>               end
>>
>>               def destroy
>>                   @feed_like_counter= Feed.find(params[:feed_id])
>>                   @feed_likes = FeedLike.where(feed_like_params)
>>                   @f = @feed_like_counter.like_count
>>                   @feed_like_counter.like_count = @f-1
>>                   @feed_like_counter.save
>>                   respond_to do |format|
>>                        if @feed_likes.destroy_all
>>                        format.html { redirect_to root_url, notice: 'Unlike ' 
>> }
>>                        format.js
>>                        else
>>
>>                        end
>>                    end
>>               end
>>               def feed_like_params
>>                   params.permit(:user_id, :feed_id)
>>                   #params[:market_place]
>>                 end
>>             def get_feed
>>               @feed= Feed.find(params[:feed_id])
>>             end
>>
>>               end
>>
>> And in views my link is like this :
>>
>>   <div class="feed-like-<%= @feed %> " >
>>
>>               <%= link_to "like",{ :action => 'create', :controller => 
>> 'feed_likes', :feed_id => @feed, :user_id => current_user.id, :remote => 
>> true }, method: :post,class: "btn btn-primary"   %>
>>
>>                   </div>
>>
>> And dislike link is like this :
>>
>>         <div class="feed-like-<%= @feed %> " >
>>              <%= link_to "Dislike",{ :action => 'destroy', :controller => 
>> 'feed_likes', :feed_id => @feed, :user_id => current_user.id, :remote => 
>> true }, class: "btn btn-primary" %>
>>
>>               </div>
>>
>> And my routes is like :
>>
>>    get "/feed_likes/:feed_id/feed_likes/:user_id" => "feed_likes#destroy"
>>       post "/feed_likes/:feed_id/feed_likes/:user_id" => "feed_likes#create"
>>
>> Here the issue is whenever someone wants to like the feed when I passes
>> the url direclty it updated the database how can I restrict this only when
>> user click the button only then it update the database not by url :
>>
>> There is another issue with this I am using ajax onclick event it updated
>> the database but when I click the like button fast it update the databse 2
>> or 3 times before the dislike partial appear . Is there any way I can use
>> this .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/rubyonrails-talk/UQznBN9IU_s/unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/df9d2950-e532-4104-b7a5-5b2d65956716%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/df9d2950-e532-4104-b7a5-5b2d65956716%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAPGhMx7n3uxFZ71LyYpqtiNz20-7%3DO%3DaS3yV70KkNuxcojZ8hw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to