0

我正在开发一个模拟基本 Facebook 应用程序的 Team Treehouse Ruby on Rails 应用程序。

我现在添加了社交功能,就像朋友一样。

我最近在我的一个观点中添加了以下链接:

<%= link_to "Add Friend", new_user_friendship_path(friend_id: @user), class: 'btn' %>

单击结果按钮时,出现以下错误:

Routing Error

uninitialized constant UserFriendshipsController
Try running rake routes for more information on available routes.

我认为问题出在“config/routes.rb”文件的“user_friendship_controller.rb”中。

这是我的“user_friendship_controller.rb 文件:

class UserFriendshipsController < ApplicationController
  before_filter :authenticate_user!, only: [:new]

  def new
    if params[:friend_id]
      @friend = User.where(profile_name: params[:friend_id]).first
      @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
end

这是我的“配置/路由”文件:

Treebook::Application.routes.draw do
  as :user do
    get '/register', to: 'devise/registrations#new',via: :get, as: :register
    get '/login', to: 'devise/sessions#new', via: :get, as: :login
    get '/logout', to: 'devise/sessions#destroy', via: :delete, as: :logout
  end

  devise_for :users, :skip => [:sessions]

  as :user do
    get '/login' => 'devise/sessions#new', as: :new_user_session
    post '/login' => 'devise/sessions#create', as: :user_session
    delete '/logout' => 'devise/sessions#destroy', as: :destroy_user_session
  end

  resources :user_friendships do
  end

  resources :statuses
  get 'feed', to: 'statuses#index', as: :feed
  root to: 'statuses#index'

  get '/:id', to: 'profiles#show', as: 'profile'
end

找出这个错误的任何帮助都是很大的帮助。

谢谢

4

1 回答 1

1

看起来这只是文件的简单错误命名。

尝试将其重命名为user_friendships_controller.rb. Rails 期望您的类声明与文件名匹配(就目前而言,它会寻找您来定义UserFriendshipController)。

于 2013-10-21T02:11:36.167 回答