无法找到ID = 151的用户

时间:2014-08-03 01:57:24

标签: ruby-on-rails ruby activerecord ruby-on-rails-4 indexing

我现在无法'接受'朋友请求并通过User_frienships index.html(朋友页面)上的链接删除它们。我收到以下错误消息。

谢谢。

UserFriendshipsController#edit

中的ActiveRecord :: RecordNotFound

找不到ID = 151的用户

User_friendship Controller

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

User_friendship Model

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

User_friendships索引页

<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 %>

2 个答案:

答案 0 :(得分:0)

在您的编辑操作中尝试此操作:

  def edit
    @friend = User.where(profile_name: params[:id]).first
    @user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first.decorate
  end

答案 1 :(得分:0)

我明白了。根据我的代码,它应该是:

def edit
    @friend = User.where(params[:id]).first
    @user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first.decorate
  end

感谢所有评论的人。