【问题标题】:undefined method `decorate' for nil:NilClassnil:NilClass 的未定义方法“装饰”
【发布时间】:2014-08-04 17:10:21
【问题描述】:

我收到以下错误

UserFriendshipsController 中的 NoMethodError#edit nil:NilClass 的未定义方法 `decorate'

我不知道如何解决它。

user_friendships 控制器

    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]).first
      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.find_by(friend_id: @friend.id).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

【问题讨论】:

  • 简单的解决方案,def nil.decorate *args; tap { "This nil is now decorated!" } end.

标签: ruby ruby-on-rails-4 undefined decorator nomethoderror


【解决方案1】:

问题是查询:

@user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first

它返回零。检查是否

  • 您从 params[:id] 获得的 id 是正确的
  • 与传递的@friend.id 存在 current_user user_friendship 关系。

编辑:

检查前面的查询是否正确:

@friend = User.where(params[:id]).first

正如 ABMagil 所说,这是不正确的。实际上,我只是在我的控制台中进行了测试,如果我像这样搜索它会引发异常:

u = User.where('1').first

因为它需要您正在搜索的字段。尝试更改为:

@friend = User.find(params[:id])

.find 搜索 id 字段并返回单个记录。如果要查找不是 id 的单个记录,请使用:

@friend = User.find_by(my_table_field: params[:id])

【讨论】:

  • 我检查了一下,看起来一切都正确。我正在跟随一个树屋教程。他们的代码和我的唯一区别是他们使用 profile_name。 @friend = User.where(profile_name: params[:id]).first 我使用:@friend = User.where(params[:id]).first 我不确定还要寻找什么。我很茫然。
  • @friend = User.where(params[:id]).first 只会返回您的第一条用户记录,因为您实际上并没有进行搜索。这相当于 SQL 查询"SELECT 'users'.* FROM 'users' WHERE (4) ASC LIMIT 1""...WHERE (4)" 部分将始终返回 true,因此您将选择所有用户,然后获取第一个用户。
  • 没错,在这种情况下,您应该使用 .find 而不是 .where。我编辑了我的答案。
  • 同样,Rails 4 中不再需要 where(...).first。请改用 find_by
  • 您在此处粘贴的 destroy 方法表明您正在尝试删除 user_friendship 关系而不是用户。为什么您的错误显示您正在尝试删除用户?我认为您应该更新您的问题或创建一个新问题。
猜你喜欢
  • 2011-07-27
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多