未定义的方法`装饰'为零:NilClass

时间:2014-08-04 17:10:22

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

我收到以下错误

UserFriendshipsController#edit中的NoMethodError 未定义的方法`装饰'为零:NilClass

我不确定如何解决它。

user_friendships 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]).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

1 个答案:

答案 0 :(得分:3)

问题是查询:

@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])