我只在Web測試中出現此錯誤。通過命令行測試每一行,一切正常。被請求的,被接受的朋友 - 一切都會被正確顯示和添加。但是,當我嘗試使用web表單,我總是得到,在控制器指着堆棧層次過深系統誤差和傳遞正確的參數:{「ID」 =>「3」}堆棧級別FriendshipsController中的SystemStackError太深#請求
User.rb
has_many :friendships
has_many :friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'accepted'",
:order => :created_at
has_many :requested_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'requested'",
:order => :created_at
friendship.rb
class Friendship < ActiveRecord::Base
attr_accessible :user, :friend, :status, :user_id, :friend_id
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => 'friend_id'
def self.request(user, friend)
return false if are_friends(user, friend)
return false if user == friend
f1 = self.new(:user_id => user.id, :friend_id => friend.id, :status => "pending")
f2 = self.new(:user_id => friend.id, :friend_id => user.id, :status => "requested")
transaction do
f1.save
f2.save
end
end
def self.are_friends(user, friend)
return false if user == friend
return true unless find_by_user_id_and_friend_id_and_status(user, friend, "accepted").nil?
return true unless find_by_user_id_and_friend_id_and_status(friend, user, "accepted").nil?
false
end
def self.are_friends_pending(user, friend)
return false if user == friend
return true unless find_by_user_id_and_friend_id_and_status(user, friend, "pending").nil?
return true unless find_by_user_id_and_friend_id_and_status(friend, user, "requested").nil?
false
end
FriendshipsController
def request
@user = current_user
@friend = User.find(params[:id])
unless @friend.nil?
if Friendship.request(@user, @friend)
flash[:notice] = "friend request sent"
else
flash[:notice] = "friend request could not be sent"
end
end
redirect_to :back
end
_userlist.html.erb
<div>
<ul>
<% User.each do |f|%>
<li>
<%= link_to "#{f.full_name}", user_path(f.id)%>
<% if Friendship.are_friends(current_user.id,f.id)%>
<label class="label">is a friend</label>
<%else%>
<%= link_to "Add Friend", addfriend_friendships_path(:id => f.id)%>
<%end%>
</li>
<%end%>
</ul>
</div>
的routes.rb
resources :friendships do
collection do
get 'request',:as=>"addfriend"
get 'accept',:as=>"accept_fr"
get 'reject',:as=>"reject_fr"
end
end
因爲一切都在命令行工作我真的不明白,爲什麼我得到這個錯誤。任何人都可以看到這個問題?
你能請發表您的PARAMS? – Mattherick
請求 參數: {「ID」 =>「3」}它傳遞正確的ID,約晚重播 – Richardlonesteen
您缺少'are_friends'方法定義對不起 - 可以請你加了嗎? – PinnyM