RyanB の railcast 163 を参照して、手紙への自己言及的関連付けを確認しましたが、次のエラーが引き続き発生します。
NoMethodError in Users#show
Showing /Users/markwalker/welcomepie/app/views/shared/_suggested_connections.html.erb where line #6 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #6):
3: <ul>
4: <% for friendship in @user.friendships %>
5: <li>
6: <%= h friendship.friend.name %>
7: (<%= link_to "remove", friendship, :method => :delete %>)
8: </li>
9: <% end %>
これが私のビューです(ユーザー/ショーページでレンダリングされる部分です):
<div class="span5">
<div class="MemberDisplay">
<ul>
<% for friendship in @user.friendships %>
<li>
<%= h friendship.friend.name %>
(<%= link_to "remove", friendship, :method => :delete %>)
</li>
<% end %>
</ul>
</div>
</div>
私の友情モデル:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
attr_accessible :friend_id, :user_id
end
私の友情コントローラー:
class FriendshipsController < ApplicationController
def create
@friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
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
end
end
私のユーザーモデル:
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
rolify
# 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 :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
attr_accessible :occupation_list, :gender_list, :moving_from_list, :moving_to_list, :family_type_list, :age_bracket_list, :interest_list, :about_me, :username, :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
acts_as_taggable
acts_as_taggable_on :occupation, :gender, :moving_from, :moving_to, :family_type, :age_bracket, :interests
scope :by_join_date, order("created_at DESC")
プライベート
def self.tag_tokens(query)
tags = User.categories.where("name like ?", "%#{query}%")
if tags.empty?
[{id: "#{query}", name: "#{query}"}]
else
tags
end
end
end
私のusers_controller:
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
authorize! :index, @user, :message => 'Not authorized as an administrator.'
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
redirect_to users_path, :notice => "Welcome to WelcomePie!"
else
redirect_to new_users_path, :alert => "Please try signing up again"
end
end
def edit
end
def update
authorize! :update, @user, :message => 'Not authorized as an administrator.'
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :admin)
redirect_to user, :notice => "User updated."
else
redirect_to user, :alert => "Unable to update user."
end
end
def destroy
authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, :notice => "User deleted."
else
redirect_to users_path, :notice => "Can't delete yourself."
end
end
def find_tags
@tags = Video.tag_tokens(params[:q])
respond_to do |format|
format.html
format.json { render json: @tags }
end
end
終わり
そして私のルート:
Welcomepie::Application.routes.draw do
resources :friendships
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users, :controllers => { :registrations => :registrations }
resources :users
end
友情.友人.名前を認識しない理由について誰か考えがありますか? それはDeviseと関係があると思いますが、うまくいきません。
助けてくれてありがとう!!