ytsejam
ytsejam

Reputation: 3439

Unique friendship help needed Rails 3

I need help about friendship uniqueness and add to friend links my routes.rb:

devise_for :users

resources :users, only: [:index, :show] do
          resources :friendships, only: [:create, :destroy]
          end

my User model :

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  # attr_accessible :title, :body
  has_many :topics
  has_many :posts
  has_many :friendships
    has_many :friends, :through => :friendships
    has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
    has_many :inverse_friends, :through => :inverse_friendships, :source => :user
end

my friendship model :

class Friendship < ActiveRecord::Base
  attr_accessible :friend_id, :user_id
  belongs_to :user
    belongs_to :friend, :class_name => "User"
end

friendships_controller.rb:

class FriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def create
        @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
        if @friendship.save
            flash[:notice] = "Arkadaşlara eklendi."
            redirect_to root_url
        else
            flash[:error] = "Arkadaşlara Eklenemiyor."
            redirect_to root_url
        end
    end

    def destroy
        @friendship = current_user.friendships.find(params[:id])
        @friendship.destroy
        flash[:notice] = "Arkadaşlarımdan kaldırıldı."
        redirect_to current_user
    end
end

users_controller.rb:

class UsersController < ApplicationController
    before_filter :authenticate_user!

  def index
        @users = User.all
  end
  def show
    @user = User.find(params[:id])
    @topics = @user.topics.paginate(page: params[:page])
    @friendship = @user.friendships.build(:friend_id => params[:friend_id])
    @friendships = @user.friendships.all

  end 
end

This is my friend add link :

 <h1>
        <%= @user.username %>
      </h1>

            <%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %>

And I cant find the correct path for showing users as friends.

<%  for friendship in @friendships %> 
<%= link_to friendship.friend.username, '#' %>
(<%= link_to 'Sil', friendship, :method => :delete %>)
<% end %>

Can anyone help me ?

Upvotes: 0

Views: 174

Answers (1)

mylescarrick
mylescarrick

Reputation: 1680

Given the nested routes it's going to need to know the user in order to get the friendship. You can use the cool helpers that get you a path from the objects

A little more idiomatically ruby-like

<%  @friendships.each do |friendship} %> 
  <%= link_to friendship.friend.username, '#' %>
  (<%= link_to 'Sil', [@user, friendship], :method => :delete %>)
<% end %>

It'll be similar for the "add to friends" link... but I'm not clear on the logic - which @user do you have at this stage? Is @user the logged-in user, or the "current" user?

Since you're using Devise... it's likely that what you really want above is:

<%  current_user.friendships.each do |friendship} %> 
  <%= link_to friendship.friend.username, '#' %>
  (<%= link_to 'Sil', [current_user, friendship], :method => :delete %>)
<% end %>

If this snippet is from another user's page... then it's something more like

<h1><%= @user.username %></h1>

<%= link_to "Arkadaşlarıma Ekle", user_friendships_path(current_user, :friend_id => @user), :method => :post, class: "btn btn-large btn-primary" %>

Upvotes: 1

Related Questions