1

我希望用户(x)能够在用户(x)在用户(y)的个人资料页面上时将另一个用户(y)添加为朋友。我设置了一个 has_many_through 并且一切正常,除了我只能从用户索引视图中添加一个朋友。提前谢谢你......代码如下:

还:

我想将“朋友”链接放在 view/profile/show.html.erb 上。当我将@users = User.all 添加到现有的profiles_controller.rb 中时,我收到了错误- undefined method 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 methodinverse_friends' for nil: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 %>

出现以下错误:

    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'

其余代码如下。

友谊.rb

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

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

用户.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

友谊控制器.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

profile_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

路线.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'

  . . .
4

2 回答 2

0

每当您尝试调用没有实际对象的方法时,您都会得到“未定义的方法”。

这意味着该方法已定义 - 但您有一个“nil”并试图在其上调用它,并且该方法对于“nil”对象不存在。

请检查您的实际用户表。您需要与用户一起工作。请确认您有一些。

如有必要,您可以使用(在script/rails console)创建用户

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

你也可以把它放在你的db/seeds.db文件中,这样你就可以在你rake db:seed第一次在新机器上设置应用程序时运行它。

于 2012-10-01T11:31:07.207 回答
0

你没有设置@usersProfilesController#show

for object in collection只是调用collection.each do |object|,这就是你得到的原因undefined method 'each' for NilClass(也是为什么通常不鼓励使用这种语法,因为它会产生像这样的令人困惑的错误)。

profile_controller.rb

def show
  @users = User.all
  #...
end
于 2012-10-01T03:25:19.390 回答