为什么@ follow.email未定义?

时间:2014-10-27 23:48:01

标签: ruby-on-rails email friend

我正在尝试为我的应用创建一个友情系统。

我正在使用active_relationships,passive_relationships,state_machine以及关注者的概念,关注者,追随者和追随者。

我已经让自己理解了它们如何融合在一起。寻找帮助找到清楚我如何使用我创建的访问朋友ID以查看朋友个人资料页面,发送电子邮件的朋友等。

突出我正在努力理解的事物: 目前我面临的是:

test_should_send_request_email#RelationshipTest
 NoMethodError: undefined method `email'
            app/mailers/user_mailer.`rb:38:in `relationship_requested'
            app/models/relationship.rb:11:in `send_request_email'
            test/models/relationship_test.rb:33:in `block (2 levels) in <class:RelationshipTest>'
            test/models/relationship_test.rb:32:in `block in <class:RelationshipTest>'

我原以为@ followed.email是一个有效的方法,并且无法解决为什么它被认为是未定义的。我(用户/关注者)想要通过电子邮件向其他用户发送电子邮件(随后)向他们发送关系请求。为什么我可以将电子邮件发送到@ followed.email?

模型/ user.rb:

class User < ActiveRecord::Base
  has_one :profile, dependent: :destroy
  has_many :active_relationships,   class_name:  "Relationship",
                                    foreign_key: "follower_id",
                                    dependent:   :destroy
  has_many :passive_relationships,  class_name:  "Relationship",
                                    foreign_key: "followed_id",
                                    dependent:   :destroy                                
  has_many :following, through: :active_relationships,  source: :followed  
  has_many :followers, through: :passive_relationships, source: :follower   

模型/ relationship.rb:

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true

  state_machine :state, initial: :pending do
  end

  def send_request_email
    UserMailer.relationship_requested(id).deliver
  end
end

user_mailer.rb:

def relationship_requested(relationship_id)
    relationship = Relationship.find(relationship_id)

    @user = relationship.follower
    @followed = relationship.followed

    mail to: @followed.email,
          subject: "#{@user.name} wants to friend you"
  end
end

relationship_requested.html.erb:

Hi <%= @followed.name %>, 

<%= @user.name %> wants to friend you.

db tables:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps
    end
    add_index :relationships, :follower_id
    add_index :relationships, :followed_id
    add_index :relationships, [:follower_id, :followed_id], unique: true
  end
end

class AddStateToRelationships < ActiveRecord::Migration
  def change
    add_column :relationships, :state, :string
    add_index :relationships, :state
  end
end

relationship_test.rb:

require 'test_helper'

class RelationshipTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true
  # end

  def setup
    @relationship = Relationship.new(follower_id: 1, followed_id: 2)
  end

  test "should be valid" do
    assert @relationship.valid?
  end

  test "should require a follower_id" do
    @relationship.follower_id = nil
    assert_not @relationship.valid?
  end

  test "should require a followed_id" do
    @relationship.followed_id = nil
    assert_not @relationship.valid?
  end

  test "should state pending until other user can accept or reject request" do
    assert_equal 'pending', @relationship.state
  end

  test "should send request email" do
    @relationship = Relationship.create(follower_id: 1, followed_id: 2)
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      @relationship.send_request_email
    end
  end
end

1 个答案:

答案 0 :(得分:0)

您的测试正在创建一个关系记录,并为关注者和关注者传递两个ID。但这些ID实际上是否与用户记录相对应?它看起来不像,因为你的测试没有创建用户(你有这两种用途的固定装置吗?)

因此,跟随者和以下关联是零,即使关系是“有效”(您的验证检查是否存在外键,但不是实际的用户实例)。

将一些调试信息放入测试中(“put”到控制台或使用Pry暂停执行测试和询问变量)。

我还建议使用FactoryGirl和Faker gems来生成测试数据。