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="#<Follow:0x103ce81d8>" 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