我正在将我的应用从rails3升级到rails4。 在friendship.controller.rb我有这条线 @friend = User.find(params [:friend_id]) 当然,这应该识别具有对应于值的id的朋友 友情表中的friend_id。 它返回一个空白。 User.find(params [:id])返回340,这是友谊的id
有什么想法吗?
class FriendshipController < ApplicationController
include ProfileHelper
before_filter :setup_friends
def create
Friendship.request(@user, @friend)
user = @user
friend = @friend
#MakeMailer.friend_request(user,friend).deliver
flash[:notice] = "Friend request sent."
redirect_to user_path(@user.id)
end
def accept
if @user.requested_friends.include?(@friend)
Friendship.accept(@friend, @user)
flash[:notice] = "Friendship with #{@friend.login} accepted!"
else
flash[:notice] = "No friendship request from #{@friend.login}."
end
redirect_to user_path(@user.id)
end
def decline
if @user.requested_friends.include?(@friend)
Friendship.breakup(@user, @friend)
flash[:notice] = "Friendship with #{@friend.login} declined"
else
flash[:notice] = "No friendship request from #{@friend.login}."
end
redirect_to user_path(@user.id)
end
def cancel
if @user.pending_friends.include?(@friend)
Friendship.breakup(@user, @friend)
flash[:notice] = "Friendship request canceled."
else
flash[:notice] = "No request for friendship with #{@friend.login}"
end
redirect_to user_path(@user.id)
end
def delete
if @user.friends.include?(@friend)
Friendship.breakup(@user, @friend)
flash[:notice] = "Friendship with #{@friend.login} deleted!"
else
flash[:notice] = "You aren't friends with #{@friend.login}"
end
redirect_to user_path(@user.id)
end
private
def setup_friends
@user = @current_user
@friend = User.where(:id => params[:friend_id])
end
end
答案 0 :(得分:0)
User.find(id_of_user)
方法默认使用用户ID,而不是任何其他属性,如果您想使用其他参数进行搜索,可以使用where
或find_by_attribute
。
User.where(:friend_id => params[:friend_id])
或
User.find_by_friend_id(params[:friend_id)