ruby on rails - Setup so users can follow(bookmark) other users -
i have created 2 different sets of coding suppose allow user follow (bookmark) user. example user follows user b, c, & d. user goes favorites page , lists users they're following (b,c & d). 1 way follow. code created did not perform action of following user, decided follow railstutorial twitter following concept. getting "uninitialized constant relationshipscontroller". wrong routes.
can take , perhaps point out wrong?
on side note, feels lot of coding simple...even when doing myself without following tutorial. want users able bookmark users page url, have saved favorites page (essentially bookmark page) , give them option remove bookmark. think database wouldn't required this.
routes:
resources :sessions, only: [:new, :create, :destroy] resources :relationships, only: [:create, :destroy] resources :users 'settings', on: :member end
user model:
has_many :notifications has_many :users, dependent: :destroy has_many :relationships, foreign_key: "follower_id", dependent: :destroy has_many :followed_users, through: :relationships, source: :followed has_many :reverse_relationships, foreign_key: "followed_id", class_name: "relationship", dependent: :destroy has_many :followers, through: :reverse_relationships, source: :follower def following?(other_user) relationships.find_by(followed_id: other_user.id) end def follow!(other_user) relationships.create!(followed_id: other_user.id) end def unfollow!(other_user) relationships.find_by(followed_id: other_user.id).destroy! end
i'd go route:
resources :sessions, only: [:new, :create, :destroy] resources :users 'settings', on: :member post 'follow', on: :member post 'unfollow, on: :member end
in users controller:
def follow friend = user.find params[:user_id] current_user.follow! friend unless current_user.following? friend redirect_to friend end def unfollow friend = user.find params[:user_id] current_user.unfollow! friend redirect_to friend end
and when displaying user want follow/unfollow
<% if current_user.following? @user %> <%= link_to 'bookmark', follow_user_path(user_id: @user.id), method: :post %> <% else %> <%= link_to 'remove bookmark', unfollow_user_path(user_id: @user.id), method: :post %> <% end %>
Comments
Post a Comment