RelationshipsController中的NoMethodError#编辑未定义的方法`decorate'为零:NilClass

时间:2014-11-07 04:50:50

标签: ruby-on-rails variables decorator nomethoderror

所以我知道问题出在哪里,但我对ROR相当新,并且不知道如何找出可用的方法/我应该使用哪些变量。

我想做的事情:

当用户按下“编辑关系”时,应将其定向到观看/关系/编辑。 变量应该对应,以便编辑关系页面处理current_user与他们尝试连接的人之间的接受,待定或请求的关系(紧随其后)

  1. 如果任何状态存在关系,请显示编辑按钮。 (作品)
  2. 点击编辑按钮(这是出现错误的地方)
  3. 显示当前用户与他们尝试连接的人之间关系的编辑页面。 (哪里可以接受/删除请求)
  4. 我不知道为什么它说没有装饰方法 - 它之前就有用了..

    错误:

    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
    

    用户数据库表: user db table

    关系控制器:

    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
    

2 个答案:

答案 0 :(得分:1)

您正在按名称找到用户,但是为条件传递&#34; ID&#34; ,因此无法检索相应的记录。更改此项并尝试一次:

 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指定为待处理关系的外键,请尝试使用上述编辑方法。我已经更新了它。

答案 1 :(得分:0)

有人说,使用params [:id]当前用户不存在active_relationship。