【问题标题】:NoMethodError in RelationshipsController#edit undefined method `decorate' for nil:NilClass关系控制器中的 NoMethodError#edit 未定义的方法 `decorate' for nil:NilClass
【发布时间】:2014-11-07 04:50:50
【问题描述】:

所以我知道问题出在哪里,但我对 ROR 还很陌生,不知道如何确定哪些方法可用/我应该使用哪些变量。

我正在尝试做的事情:

当用户按下“编辑关系”时,应被引导至视图/关系/编辑。 变量应该对应,以便编辑关系页面处理 current_user 和他们尝试连接的人之间的接受、待处理或请求的关系(关注)

  1. 如果在任何状态下都存在关系,则显示编辑按钮。 (作品)
  2. 点击编辑按钮(这是出现错误的地方)
  3. 显示当前用户和他们试图连接的人之间关系的编辑页面。 (在哪里可以接受/删除请求)

我不知道为什么它说没有装饰器方法 - 它以前工作过..

错误:

NoMethodError in RelationshipsController#edit
undefined method `decorate' for nil:NilClass

Extracted source (around line #71):

    # @relationship = current_user.active_relationships.find(params[:id]).decorate
    @followed = User.find_by(name: params[:id])
    @relationship = current_user.pending_relationships.find_by(followed_id: @followed).decorate
  end

查看/用户/索引:

<% if logged_in? %>
    <ul>
        <% @users.each do |user| %>
            <li>
                <%= user.name %>
                <div id="relationship-status">
                    <% if current_user.following.include?(@followed) || current_user.pending_following.include?(user) || current_user.requested_following.include?(user) %>
                        <%= link_to "Edit Relationship", edit_relationship_path(followed_id: @followed, id: current_user.id), class: "btn btn-primary" %>
                        followed: <%= @followed %>
                        current_user: <%= current_user.id %>
                        relationship: <%= @relationship %>
                    <% else %>
                        <%= link_to "Add Relationship", new_relationship_path(follower_id: user.id), class: "btn btn-primary", id: 'add-relationship', data: { followed_id: user.id.to_param } %>
                    <% end %>
                </div>
            </li>
        <% end %>
    </ul>
<% end %>   

relationships_controller:

  def edit
    # @followed = @relationship.followed
    # @relationship = current_user.active_relationships.find(params[:id]).decorate
    @followed = User.find(name: params[:id])
    @relationship = current_user.pending_relationships.find_by(followed_id: @followed).decorate
  end

观点/关系/编辑:

    <div class="page-header">
        <h1>Viewing Relationship</h1>
    </div>

    <h3><%= @relationship.sub_message %></h3>

    <div class="form-actions">
        <% if @relationship.requested? %>
            <%= form_for @relationship, url: accept_relationship_path(@relationship), method: :put do |form| %>
                <%= submit_tag "Accept Relationship", class: 'btn btn-primary' %>
            <% end %>
        <% end %>
    </div>

    <%= form_for @relationship, url: relationship_path(@relationship), method: :delete do |form| %>
        <%= submit_tag "Delete Relationship", class: 'btn btn-danger' %>

<% end %>

型号/用户:

class User < ActiveRecord::Base
  has_one :profile, dependent: :destroy
  has_many :pending_relationships,  class_name:  "Relationship",
                                    foreign_key: "follower_id"
  has_many :active_relationships,   class_name:  "Relationship",
                                    foreign_key: "follower_id",
                                    dependent:   :destroy
  has_many :passive_relationships,  class_name:  "Relationship",
                                    foreign_key: "followed_id",
                                    dependent:   :destroy                                
  has_many :following, -> { where(relationships: { state: "accepted" } ) }, through: :active_relationships,  source: :followed
  has_many :followers, through: :passive_relationships, source: :follower                              
  has_many :pending_following, -> { where(relationships: { state: "pending" } ) }, through: :pending_relationships,  source: :followed
  has_many :requested_following, -> { where(relationships: { state: "requested" } ) }, through: :pending_relationships,  source: :followed

...

 # Follows a user.
  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

  # Unfollow a user. 
  def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
  end

  # Return true if the current user is following the other user. 
  def following?(other_user)
    following.include?(other_user)
  end

  def pending_following?(user)
    pending_following.include?(user)
  end

  def requested_following?(user)
    pending_following.include?(user)
  end

用户数据库表:

关系控制器:

class RelationshipsController < ApplicationController
  before_action :logged_in_user,  only: [:new, :create, :index, :accept, :edit, :destroy]
  respond_to :html, :json

  def new
    if params[:followed_id]
      @followed = User.find(params[:followed_id])
      @active_relationship = current_user.active_relationships.new(followed: @followed)
    else
      flash[:danger] = "Relationship required"
    end

  rescue ActiveRecord::RecordNotFound 
    render 'public/404', status: :not_found
  end

  def create
    if params[:relationship] && params[:relationship].has_key?(:followed_id)
      @followed = User.find(params[:relationship][:followed_id])
      # @followed = User.where(name: params[:relationship][:followed_id]).first
      @relationship = Relationship.request(current_user, @followed)
      respond_to do |format|
        if @relationship.new_record?
          format.html do
            flash[:danger] = "There was a problem creating that relationship request"
            redirect_to followed_path(@followed)
          end
          format.json { render json: @relationship.to_json, status: :precondition_failed }
        else
          format.html do
            flash[:success] = "Friend request sent"
            redirect_to followed_path(@followed)
          end
          format.json { render json: @relationship.to_json }
        end
      end
    else
      flash[:danger] = "Friend Required"
      redirect_to users_path
    end
  end

  # def create
  #   if params[:followed_id]
  #     @followed = User.find(params[:followed_id])
  #     current_user.follow(@followed)
  #     redirect_to user
  #   else 
  #     flash[:danger] = "else statement"
  #   end
  # end

  def accept
    @relationship = current_user.active_relationships.find(params[:id])
    if @relationship.accept!
      flash[:success] = "You are now connected with #{@relationship.followed.name}"
    else
      flash[:danger] = "That connection could not be accepted."
    end
    redirect_to relationships_path
  end

  def index
    @relationships = current_user.active_relationships.all
    @followed = User.find_by(name: params[:id])
  end

  def edit
    #orig
    # @followed = @relationship.followed
    # @relationship = current_user.active_relationships.find(params[:id]).decorate

    #2nd
    # @followed = User.find_by(name: params[:id])
    # @relationship = current_user.pending_relationships.find_by(followed_id: @followed).decorate

    # stack
    # @followed = User.find_by(id: params[:id])
    # @relationship = current_user.pending_relationships.find_by(followed_id: @followed).decorate

    #stack2
    @followed = User.find_by(id: params[:id])
    @relationship = current_user.pending_relationships.find_by(follower_id: @followed.id).decorate
  end

  def destroy
   ...
  end
end

【问题讨论】:

    标签: ruby-on-rails variables decorator nomethoderror


    【解决方案1】:

    您正在查找 users by name,但为条件传递 "ID",因此无法检索相应的记录。更改此设置并尝试一次:

     def edit
        # @followed = @relationship.followed
        # @relationship = current_user.active_relationships.find(params[:id]).decorate
        @followed = User.find_by(id: params[:id])
        @relationship = current_user.pending_relationships.find_by(follower_id: @followed.id).decorate
      end
    

    更新:

    您已将follower_id 指定为待处理关系的外键,请尝试上述编辑方法。我已经更新了。

    【讨论】:

    • 'RelationshipsController#edit 中的类型错误 - 无法将 Hash 转换为整数'
    • 如果我确实 find_by 我得到:RelationshipsController#edit undefined method `decorate' for nil:NilClass
    • @sss333 一旦我编辑了我的答案,检查一下,你的用户模型中是否有 follow_id。如果可能,请在此处添加您的用户属性
    • 我怎么知道您何时完成了答案的编辑?我有它,包括 find_by 编辑。将用户模型添加到原始问题中
    • @sss333 Nice.. 要查询数据库,您必须在该表中有字段,因此您的字段名称为 follower_id 但不是 followed_id,所以您遇到了错误。
    【解决方案2】:

    意思是,使用 params[:id] 时,当前用户不存在 active_relationships。

    【讨论】:

    • 抱歉,我不确定您所说的 @pooja agarwal 是什么意思?
    • current_user.active_relationships.find(params[:id]) 为零。这就是为什么它为 nil 类显示未定义的方法“装饰”。
    • 你的意思是pending_relationships? (活动被注释掉)为什么它说零?我在数据库中有待处理的关系。我如何使它不为零?
    • 用@followed 检查表中是否有任何数据的id。
    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 2013-04-24
    • 1970-01-01
    • 2013-11-14
    • 2013-01-14
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多