1

사용자 (x)가 사용자 (y) 프로필 페이지에있는 동안 사용자 (x)가 친구로 다른 사용자 (y)를 추가 할 수있게하려고합니다. 나는 has_many_through를 설정하고 User Index View에서만 친구를 추가 할 수 있다는 것을 제외하면 모든 것이 작동합니다. 사전에 감사합니다 ... 코드는 아래와 같습니다 또한프로필 작성 중 친구 만들기

:

내가보기/프로필/show.html.erb의 "친구"링크를 배치하고 싶었다. NilClass

UserIndexView 작동 코드가 아니라 ProfileShowView :

% for user in @users %> 
    <div class="user"> 
    <p> 
     <strong><%=h user.email %> <%= user.id %></strong> 
     <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post%> 
    <div class="clear"></div> 
    </p> 
    </div> 
<% end %> 

을 전무에 대한 정의되지 않은 방법 friendships' for nil:NilClass. When I replaced @user = User.find(params[:id]) with @users = User.all I received the error - NoMethodError in Profiles#show... undefined method inverse_friends '- 나는 기존 profiles_controller.rb에 @users = User.all 추가 할 때 나는 오류가 발생했습니다 다음 오류가 발생합니다.

NoMethodError in Profiles#show 
    Showing /Users/mgoff1/LOAP_1.2.2/app/views/profiles/show.html.erb where line #13 raised: 
undefined method `each' for nil:NilClass 
Extracted source (around line #13): 
10: 
11: 
12: 
13: <% for user in @users %> 
14:  <div class="user"> 
15:   <p> 
16:   <strong><%=h user.email %> <%= user.id %></strong> 
    . . . 

app/views/profiles/show.html.erb: 13:in`_app_views_profiles_show_html_erb___2905846706508390660_2152968520' 
app/controllers/profiles_controller.rb:19:in `show' 

나머지 코드는 다음과 같습니다.

friendship.rb

class Friendship < ActiveRecord::Base 
attr_accessible :create, :destroy, :friend_id, :user_id 

belongs_to :user 
belongs_to :friend, :class_name => "User" 
end 

user.rb

class User < ActiveRecord::Base 
    has_many :friendships 
    has_many :friends, :through => :friendships 
    has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id" 
    has_many :inverse_friends, :through => :inverse_friendships, :source => :user 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes 
    # attr_accessible :title, :body 



    has_one :profile 
    accepts_nested_attributes_for :profile 

    before_save do | user | 
    user.profile = Profile.new unless user.profile 
end 
end 

friendships_controller.rb

class FriendshipsController < ApplicationController 
    def create 
    @friendship = current_user.friendships.build(:friend_id => params[:friend_id]) 
    if @friendship.save 
     flash[:notice] = "Added friend." 
     redirect_to current_user.profile 
    else 
     flash[:error] = "Unable to add friend." 
     redirect_to root_url 
    end 
    end 

    def destroy 
    @friendship = current_user.friendships.find(params[:id]) 
    @friendship.destroy 
    flash[:notice] = "Removed friendship." 
    redirect_to current_user.profile 
    end 
end 

users_controller.rb

class UsersController < ApplicationController 
    def show 
    @user = User.find(params[:id]) 
    end 

    def index 
    @users = User.all 
    end 
end 
012,351,

profiles_controller.rb

class ProfilesController < ApplicationController 
    # GET /profiles 
    # GET /profiles.json 
    def index 
    @profiles = Profile.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @profiles } 
    end 
    end 

    # GET /profiles/1 
    # GET /profiles/1.json 
    def show 
    @user = User.find(params[:id]) 
    @profile = Profile.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @profile } 
    end 
    end 

    # GET /profiles/new 
    # GET /profiles/new.json 
    def new 
    @profile = Profile.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @profile } 
    end 
    end 

    # GET /profiles/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    @profile = Profile.find(params[:id]) 
    end 

    # POST /profiles 
    # POST /profiles.json 
    def create 
    @profile = Profile.new(params[:profile]) 

    respond_to do |format| 
     if @profile.save 
     format.html { redirect_to @profile, notice: 'Profile was successfully created.' } 
     format.json { render json: @profile, status: :created, location: @profile } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /profiles/1 
    # PUT /profiles/1.json 
    def update 
    @profile = Profile.find(params[:id]) 

    respond_to do |format| 
     if @profile.update_attributes(params[:profile]) 
     format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /profiles/1 
    # DELETE /profiles/1.json 
    def destroy 
    @profile = Profile.find(params[:id]) 
    @profile.destroy 

    respond_to do |format| 
     format.html { redirect_to profiles_url } 
     format.json { head :no_content } 
    end 
    end 
end 

routes.rb

BaseApp::Application.routes.draw do 


    resources :friendships 

    resources :profiles 

    #get "users/show" 

    devise_for :users, :controllers => { :registrations => "registrations" } 
    resources :users 

    match '/show', to: 'profile#show' 




    match '/signup', to: 'users#new' 

    root to: 'static_pages#home' 

    match '/', to: 'static_pages#home' 

    . . . 

답변

0

당신은 ProfilesController#show@users 설정되지 않습니다.

for object in collectionundefined method 'each' for NilClass이되는 이유이며,이 구문과 같은 혼란스러운 오류가 발생하므로 일반적으로이 구문을 사용하지 않는 것이 좋습니다.

profiles_controller.rb

def show 
    @users = User.all 
    #... 
end 
+0

view/profile/show.html.erb에 "friend"링크를 넣으 려합니다. 기존의 profiles_controller.rb에 @users = User.all을 추가했을 때, Nil : NilClass에 대해 정의되지 않은 메소드'friendships '를 받았습니다. @user = User.find (params [: id])를 @users = User.all로 바꿨을 때 Profiles # show ...에 NoMethodError 오류가 발생했습니다 ... 정의되지 않은 메소드 inverse_friends for nil : NilClass – JLBaguio

0

당신이 '방법은 정의되지 않은'얻을 것이다 실제 객체 메소드를 호출하려고 언제.

이것은 메서드가 정의되었음을 의미합니다.하지만 'nil'이 있고이 메서드를 호출하려고하고 해당 메서드가 'nil'개체에 대해 존재하지 않습니다.

실제 사용자 테이블을 확인하십시오. 사용자가 함께 작업해야합니다. 가지고 있는지 확인하십시오.당신이

User.new(:name=>'fred', :password =>'pword', :password_confirmation => 'pword')

과 함께합니다 (script/rails console에서) 사용자를 만들 수 있습니다

필요한 경우 새에 응용 프로그램을 처음으로 설정을 rake db:seed 실행할 수 있도록 당신은 또한 당신의 db/seeds.db 파일에이를 배치 할 수 있습니다 기계.