I am trying to build a friendship system sorta following this link: How to Implement a Friendship Model in Rails 3 for a Social Networking Application? . However the code seem to be very outdated, and I might of change it, what i am trying to do atm is simply to create a relationship, but this seem to not works.
So here my create
#Send a friendship request
def create
Friendship.request(@customer, @friend)
redirect_to friendships_path
end
Which then technically would called the method request located in model which his already implemented in the previous post.
def self.request(customer, friend)
unless customer == friend or Friendship.exists?(customer, friend)
transaction do
create(:customer => customer, :friend => friend, :status => 'pending')
create(:customer => friend, :friend => customer, :status => 'requested')
end
end
end
and i also added these to the model
attr_accessible :status, :customer_id, :friend_id, :customer, :friend
However the friendship doesn't get created. Any reason why not? I call the relationship has follow
<%= link_to "Add friend", friendships_path(:friend_id => customer), :method => :post %>
You need to separate the @customer and @friend. In your link, you are setting :friend_id to the customer, and you are never setting the @customer id.
Try this:
def create
@customer = current_account
@friend = Account.find(params[:friend_id])
Friendship.request(@customer, @friend)
redirect
end
In the link_to you need:
<%= link_to "Add Friend", friendships_path(:friend_id => friend),: method => :post %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.