Ruby on rails 不能';找不到id为151的用户
我现在无法通过User_frienships index.html(朋友页面)上的链接“接受”朋友请求并删除他们。我得到下面的错误消息 多谢各位 ActiveRecord::在UserFriendsController#edit中找不到RecordNot 找不到id为151的用户 用户\u友谊控制器Ruby on rails 不能';找不到id为151的用户,ruby-on-rails,ruby,activerecord,ruby-on-rails-4,indexing,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 4,Indexing,我现在无法通过User_frienships index.html(朋友页面)上的链接“接受”朋友请求并删除他们。我得到下面的错误消息 多谢各位 ActiveRecord::在UserFriendsController#edit中找不到RecordNot 找不到id为151的用户 用户\u友谊控制器 class UserFriendshipsController < ApplicationController before_filter :authenticate_user! re
class UserFriendshipsController < ApplicationController before_filter :authenticate_user!
respond_to :html, :json
def index
@user_Friendships = current_user.user_friendships.all
end
def accept
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.accept_mutual_friendship!
@user_friendship.friend.user_friendships.find_by(friend_id : current_user.id).accept_mutual_friendship!
flash[:success] = "You are now friends with # {@user_friendship.friend.name}"
redirect_to user_friendships_path
else
flash[:error] = "That friendship could not be accepted"
end
end
def new
if params[:friend_id]
@friend = User.find(params[:friend_id])
raise ActiveRecord::RecordNotFound if @friend.nil?
@user_friendship = current_user.user_friendships.new(friend: @friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
@friend = User.find(params[:user_friendship] [:friend_id])
@user_friendship = UserFriendship.request(current_user, @friend)
respond_to do |format|
if @user_friendship.new_record?
format.html do
flash[:error] = "There was a problem creating this friend request."
redirect_to user_path(@friend)
end
format.json { render json: @user_friendship.to_json, status: :precondition_failed }
else
format.html do
flash[:success] = "Friend request sent."
redirect_to user_path(@friend)
end
format.json { render json: @user_friendship.to_json }
end
end
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
def edit
@friend = User.find(params[:id])
@user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first.decorate
end
def destroy
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.destroy
flash[:success] = "Your friendship was deleted"
end
redirect_to user_friendships_path
end
def user_friendship
params.require(:user_friendship).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
end
end
class UserFriendshipsController
用户友谊模型
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
after_destroy :delete_mutual_friendship!
state_machine :state, initial: :pending do
after_transition on: :accept, do: [:send_acceptance_email, :accept_mutual_friendship!]
state :requested
event :accept do
transition any => :accepted
end
end
def self.request(user1, user2)
transaction do
friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
friendship1.send_request_email
friendship1
end
end
def send_request_email
UserNotifier.friend_requested(id).deliver
end
def send_acceptance_email
UserNotifier.friend_request_accepted(id).deliver
end
def mutual_friendship
self.class.where({user_id: friend_id, friend_id: user_id}).first
end
def accept_mutual_friendship!
# Grab the mutual friendship and update the state without using the state machine, so as
# not to invoke callbacks.
mutual_friendship.update_attribute(:state, 'accepted')
end
def delete_mutual_friendship!
mutual_friendship.delete
end
class用户友谊:已接受
终止
终止
def自我请求(用户1、用户2)
事务处理
友谊1=创造!(用户:user1,朋友:user2,状态:“待定”)
友谊2=创造!(用户:user2,朋友:user1,状态:“已请求”)
友谊1.发送\请求\电子邮件
友谊1
终止
终止
def发送请求电子邮件
UserNotifier.friend_请求(id)。传递
终止
def发送\接收\电子邮件
UserNotifier.friend\u请求\u已接受(id)。传递
终止
相互的友谊
self.class.where({user\u id:friend\u id,friend\u id:user\u id})
终止
接受彼此的友谊!
#在不使用状态机的情况下抓取彼此的友谊并更新状态,以便
#不调用回调。
相互友谊。更新属性(:状态“已接受”)
终止
def删除彼此的友谊!
相互的友谊
终止
用户友谊索引页
<div class="page-header">
<h1> Friends </h1>
</div>
<% @user_Friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
</div>
<div class="span7">
<strong><%= friend.name %></strong><br />
<%if friendship.pending? %>
<em>Friendship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.requested? %>
<em>Friendship requested.</em> <%=link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.accepted? %>
<em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
<% end %>
</div>
</div>
<% end %>
朋友
友谊正在等待。
请求友谊。
友谊开始了。
在编辑操作中尝试以下操作:
def edit
@friend = User.where(profile_name: params[:id]).first
@user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first.decorate
end
我明白了。根据我的代码,应该是:
def edit
@friend = User.where(params[:id]).first
@user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first.decorate
end
谢谢大家的评论 错误是不言自明的,您收到此错误是因为您没有id为151的用户。您能确认您有id为151的用户吗?我只有4个用户。这就是为什么我不确定它是从哪里来的。我是一个新手,请光着身子。感谢@RichPeckThats他们在教程中的介绍,但是,我没有使用profile\u名称。使用时:@friend=User.where(profile\u name:params[:id])。首先,我得到以下错误。SQLite3::SQLException:没有这样的列:users.profile\u name:选择“users”。*从“users”中选择“users”。“profile\u name”=“2”按“users”排序。“id”ASC LIMIT 1将列profile\u name添加到您的users表中,run:rails g迁移将_profile _name _添加到_userprofile _name:string和rake db:migrate