0

I have been trying to find this answer everywhere online without luck, maybe this will help someone else.

I installed a ruby gem called socialization, which works fine.. it allows users to follow/unfollow/like/unlike/mention anything.

the issue I am having is I have made two links (one that follows and one that unfollows) the links work and when clicked the records get added and deleted from the db perfectly BUT when i hit refresh the action happens anyway without me clicking the link.

to be as clear as possible the issue is:

If user is currently followed and I refresh the page the will be unfollowed (without clicking the button).. then when they are unfollowed and I refresh the page they will now be followed again (again without clicking the button)

here is the code:

<% if current_user.follows?(@user)%>
   <%= link_to "Unfollow", user_path, :action => current_user.unfollow!(@user), :class => "btn btn-primary"%>
<% else %>
   <%= link_to "Follow", user_path, :action => current_user.follow!(@user), :class => "btn btn-primary"%>
<% end %>

I think it has something to do with either: the browser caching the links OR the fact that the link generated is

<a href="/users/1" action="#&lt;Follow:0x103ce81d8&gt;" class="btn btn-primary" rel="nofollow">Follow</a>

and the action gets executed either way

Edit:

Rake routes: 

  users_index GET    /users/index(.:format)            users#index
         dashboard_index GET    /dashboard/index(.:format)        dashboard#index
    dashboard_my_rentals GET    /dashboard/my_rentals(.:format)   dashboard#my_rentals
    dashboard_my_credits GET    /dashboard/my_credits(.:format)   dashboard#my_credits
    dashboard_my_invites GET    /dashboard/my_invites(.:format)   dashboard#my_invites
      dashboard_my_faves GET    /dashboard/my_faves(.:format)     dashboard#my_faves
  dashboard_edit_profile GET    /dashboard/edit_profile(.:format) dashboard#edit_profile
           tsmhome_index GET    /tsmhome/index(.:format)          tsmhome#index
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy
              home_index GET    /home/index(.:format)             home#index
                   users GET    /users(.:format)                  users#index
                         POST   /users(.:format)                  users#create
                new_user GET    /users/new(.:format)              users#new
               edit_user GET    /users/:id/edit(.:format)         users#edit
                    user GET    /users/:id(.:format)              users#show
                         PUT    /users/:id(.:format)              users#update
                         DELETE /users/:id(.:format)              users#destroy
                                /show/:id(.:format)               user#show
                    root        /                                 home#index



user controller

  def index
@users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :json => @users }
end


end
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :json => @user }
    end
  end
  • Problem with ur links action.. Check paths using `rake routes` for `follow` and `unfollow`. If you don't find hot to fix, post your `controller methods` and `routes details`.. – Rahul Tapali Feb 26 '13 at 15:45

1 Answers1

0

In your link action is considered as attribute of link_to tag. Because action should be given as second parameter. Check doc. For get the paths to specific action of controller do rake routes. It will list the routes and use the same with appending _path at end in your link_to tag.

I think it may look like below:

<% if current_user.follows?(@user)%>
   <%= link_to "Unfollow", unfollow_user_path(:user => @user), :class => "btn btn-primary"%>
<% else %>
   <%= link_to "Follow", follow_user_path(:user => @user), :class => "btn btn-primary"%>
<% end %>

In controller:

def follow
  #do something like
  user = User.find(params[:user])
  current_user.follow!(user)
  flash[:success] = "Follwed user #{user.name}"
  redirect_to :back
end

def unfollow
  #do something like
  user = User.find(params[:user])
  current_user.unfollow!(user)
  flash[:success] = "Unfollwed user #{user.name}"
  redirect_to :back
end

In routes:

resources :users do
  collection do
    get 'follow'
    get 'unfollow'
  end
end
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44