堆栈级别在FriendshipsController#request中太深的SystemStackError

时间:2014-01-08 17:03:06

标签: ruby-on-rails ruby-on-rails-3 stack

我只在网络测试中遇到此错误。通过命令行测试每一行,一切正常。请求的,被接受的朋友 - 所有内容都会被正确显示和添加。但是当我尝试使用网络表单时,我总是得到堆栈级别太深的系统错误指向控制器并传递正确的参数:{“id”=>“3”}

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

因为一切都在命令行中运行,所以我真的不明白为什么会出现这个错误。有谁能看到这个问题?

1 个答案:

答案 0 :(得分:0)

一些建议,

重命名您的控制器方法,request我认为它与http请求太相似,我建议request_friendship或类似。

请勿使用redirect_to :back,尝试重定向到特定路径/网址,看看是否有任何修复。

我担心问题上没有足够的信息来明确给出答案所以只是猜测......