2016-09-16 58 views
0

我正在制作我的第一个独奏栏项目。这是一个Facebook克隆,目前我无法让用户接受好友请求。如果所发布的信息不足以帮助解答问题,请告诉我。Rails:用户不能接受朋友请求

friend_request_controller.rb 

class FriendRequestsController < ApplicationController 
before_action :set_friend_request, except: [:index, :create] 

def index 
    @incoming = FriendRequest.where(friend: current_user) 
    @outgoing = current_user.friend_requests 
end 

def create 
    @user = User.find(current_user) 
    friend = User.find(params[:id]) 
    @friend_request = current_user.friend_requests.new(friend_id: friend) 

    if @friend_request.save 
     redirect_to user_path(current_user) 
    end 
end 

def update 
    friend = User.find(params[:id]) 
    @friend_request = current_user.friend_requests.find(friend_id: friend) 
    @friend_request.accept 
end 

show.html.erb 

Hello my my email is <%= @user.email %> 
<% if user_signed_in? %> 
    <li> 
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %></li> 

    <li><%= link_to 'All Users', users_path %>  
</li> 
<% else %> 
<li> 
<%= link_to('Login', new_user_session_path) %> 
</li> 
<% end %> 
<ul> 

<% current_user.friend_requests.each do |request| %> 
<h4>You have new friend requests from:</h4> 
<li> 
    <%= User.find(request.friend_id).email %> 
    <%= link_to "Accept", friend_request_path(friend_id: @friend),method:"put" %> 
    <%= link_to "Decline", "#" %> 
</li> 
<% end %> 
</ul> 

我知道什么是错在这里我的link_to助手

user.rb 

class User < ApplicationRecord 
has_many :friend_requests, dependent: :destroy 
has_many :pending_friends, through: :friend_requests, source: :friend 
has_many :friendships, dependent: :destroy 
has_many :friends, through: :friendships 
# Include default devise modules. Others available are: 
# :confirmable, :lockable, :timeoutable and :omniauthable 
devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
end 

friend_request.rb 

class FriendRequest < ApplicationRecord 
    belongs_to :user 
    belongs_to :friend, class_name: 'User' 

def accept 
    user.friends << friend 
    destroy 
end 

def destroy 
end 

end 
+0

您提供了很多代码,非常棒!但是,你提供的问题内容是“我知道这里的link_to helper出了问题”,这不是很有帮助。发生了什么,会发生什么?那条路线甚至定义了吗?你的链接看起来很好... – fbelanger

+0

当我单击接受链接时,我得到这个错误:PG :: UndefinedTable:错误:缺少表“ID”的FROM-clause条目LINE 1:... uests“ WHERE“friend_requests”。“user_id”= $ 1 AND“id”。“frie ... ^:SELECT”friend_requests“。* FROM”friend_requests“WHERE”friend_requests“。”user_id“= $ 1 AND”id“。”friend_id“ = 3限额$ 2 –

+0

您是否尝试过我的答案? – araratan

回答

0

相反的:

<%= link_to "Accept", friend_request_path(friend_id: @friend),method:"put" %> 

尝试改变@friendrequest.friend_id

<%= link_to "Accept", friend_request_path(friend_id: request.friend_id),method:"put" %> 
1

做什么@araratan在frined_request控制器已经提到+,更换PARAMS:使用参数[ID] [:friend_id]。

+0

谢谢。我抓住了第二部分,并做出了改变 –

相关问题