未定义的局部变量或方法“new_friend_requests_path”

undefined local variable or method `new_friend_requests_path'

如何向用户页面添加 "Add Friend" 按钮?我尝试使用 <%= link_to "Add Friend", new_friend_requests_path, method: :post %> 但似乎没有用。

如有任何帮助,我们将不胜感激。

FriendsController.rb

class FriendsController < ApplicationController
  before_action :set_friend, only: :destroy

  def index
    @friends = current_user.friends
  end

    def destroy  
      current_user.remove_friend(@friend)
      head :no_content
    end
  ...
  private

  def set_friend
    @friend = current_user.friends.find(params[:id])
  end
end

FriendRequestsController.rb

class FriendRequestsController < ApplicationController  
  before_action :set_friend_request, except: [:index, :create]

    def index  
      @incoming = FriendRequest.where(friend: current_user)
      @outgoing = current_user.friend_requests
    end  

  def create
    friend = User.find(params[:friend_id])
    @friend_request = current_user.friend_requests.new(friend: friend)

    if @friend_request.save
      render :show, status: :created, location: @friend_request
    else
      render json: @friend_request.errors, status: :unprocessable_entity
    end
  end

    def destroy  
      @friend_request.destroy
      head :no_content
    end

    def update  
      @friend_request.accept
      head :no_content
    end

  private

  def set_friend_request
    @friend_request = FriendRequest.find(params[:id])
  end
end  

User.rb

class User < ActiveRecord::Base
    extend FriendlyId

    friendly_id :name, use: :slugged
    has_many :friend_requests, dependent: :destroy
    has_many :pending_friends, through: :friend_requests, source: :friend
    has_many :friendships, dependent: :destroy
    has_many :friends, through: :friendships


      def remove_friend(friend)
        current_user.friends.destroy(friend)
      end

end

Friendship.rb

class Friendship < ActiveRecord::Base
  after_create :create_inverse_relationship
  after_destroy :destroy_inverse_relationship

  belongs_to :user
  belongs_to :friend, class_name: 'User'

  validates :user, presence: true  
validates :friend, presence: true, uniqueness: { scope: :user }    


  private

  def create_inverse_relationship
    friend.friendships.create(friend: user) 
  end

  def destroy_inverse_relationship
    friendship = friend.friendships.find_by(friend: user)
    friendship.destroy if friendship
  end

    def not_self  
      errors.add(:friend, "can't be equal to user") if user == friend
    end 
end

FriendRequest.rb

class FriendRequest < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User'
  validate :not_self 
    validate :not_friends
  validate :not_pending

      def accept
        user.friends << friend
        destroy
      end

private

    def not_self  
      errors.add(:friend, "can't be equal to user") if user == friend
    end  

      def not_pending
        errors.add(:friend, 'already requested friendship') if friend.pending_friends.include?(user)
      end

end

建立友谊 |迁移

class CreateFriendships < ActiveRecord::Migration
  def change
    create_table :friendships do |t|
      t.references :user, index: true
      t.references :friend, index: true

      t.timestamps null: false
    end
    add_foreign_key :friendships, :users
    add_foreign_key :friendships, :friends
  end
end

创建好友请求 |迁移

class CreateFriendRequests < ActiveRecord::Migration
  def change
    create_table :friend_requests do |t|
      t.references :user, index: true
      t.references :friend, index: true

      t.timestamps null: false
    end
    add_foreign_key :friend_requests, :users
    add_foreign_key :friend_requests, :friends
  end
end

Routes.rb

Rails.application.routes.draw do

  get 'friends/index'

  get 'friends/destroy'

  resources :friend_requests
end

佣金路线

       friends_index GET    /friends/index(.:format)                    friends#index
     friends_destroy GET    /friends/destroy(.:format)                  friends#destroy
     friend_requests GET    /friend_requests(.:format)                  friend_requests#index
                     POST   /friend_requests(.:format)                  friend_requests#create
  new_friend_request GET    /friend_requests/new(.:format)              friend_requests#new
 edit_friend_request GET    /friend_requests/:id/edit(.:format)         friend_requests#edit
      friend_request GET    /friend_requests/:id(.:format)              friend_requests#show
                     PATCH  /friend_requests/:id(.:format)              friend_requests#update
                     PUT    /friend_requests/:id(.:format)              friend_requests#update
                     DELETE /friend_requests/:id(.:format)              friend_requests#destroy

new_friend_requests_path 应该是 new_friend_request_path.

通过 运行 rake routes 检查您的应用程序的路由。

rake routes 输出我可以看出它是 new_friend_request 而不是 new_friend_requests。而且它是 GET 方法而不是 POST。所以下面的作品

<%= link_to "Add Friend", new_friend_request_path %>

更新:

改变这个

before_action :set_friend_request, except: [:index, :create]

before_action :set_friend_request, except: [:index, :new, :create]

而且你应该在你的 FriendRequestsController 中有一个 new 方法,如下所示。

def new
  @friend_request = FriendRequest.new
end