2014-02-24 54 views
0

我一直在這個問題上呆了一天半。我使用Mailboxer創建一個非常乾淨的郵件系統;其中一個與樣本相同 - https://github.com/RKushnir/mailboxer-app - 只是爲了從這個寶石開始。Mailboxer/Rails:未定義的方法'sender ='

我的字面意思是兩個存儲庫在我的電腦上彼此相鄰。郵箱特定的模型,遷移,初始化器,我可以找到的所有東西。這包括用戶模型,其中包含name操作。我甚至嘗試用attr_accessibleNotification模型搞混了。

數據庫是相同的。

當我在示例應用程序中保存對話後,在控制檯/種子中嘗試創建消息時,這很容易。一旦談話被保存,我運行:

n = Message.new 
n.sender = User.find(1) 
n.subject = 'Hi' 
n.body = 'Hello there.' 
n.conversation_id = Conversation.find(1).id 
n.save 

和繁榮。它的作品非常漂亮。另一方面,我的應用程序的功能完全相同。直到它到達sender字段。

當我嘗試設置發件人:

n.sender = User.find(1)

我收到此錯誤:NoMethodError: undefined method 'sender=' for #<Message:0x00000101708eb8>

爲什麼?

這讓我瘋狂。如果有人能夠提供一些幫助,我會很感激。先謝謝你。

消息/通知模式:

create_table "notifications", force: true do |t| 
    t.string "type" 
    t.text  "body" 
    t.string "subject",    default: "" 
    t.integer "sender_id",       null: false 
    t.integer "conversation_id" 
    t.boolean "draft",    default: false 
    t.datetime "updated_at",       null: false 
    t.datetime "created_at",       null: false 
    t.integer "notified_object_id" 
    t.string "notified_object_type" 
    t.string "notification_code" 
    t.string "attachment" 
    t.boolean "global",    default: false 
    t.datetime "expires" 
    end 

    add_index "notifications", ["conversation_id"], name: "index_notifications_on_conversation_id", using: :btree 

這是寶石產生什麼,它是相同的示例應用程序爲好。

+0

你能不能把你的消息架構代碼。它說你的消息沒有發件人字段。 – sonnyhe2002

+0

令我困擾的是,當然,沒有發件人字段 - 但是因爲遷移是't.references:sender',它創建了'sender_id'和'sender_typ'e字段,我可以通過發件人字段在示例應用程序的控制檯中。但不是我的應用 – user3181113

+0

而且,最重要的是,我嘗試刪除發件人字段並用簡單的發件人ID替換它,然後耙我的遷移,但我仍然得到相同的錯誤 – user3181113

回答

0

看看Mailboxer gem sent_message方法here你可能會覺得它有幫助。

您還可以使用內置的send_message方法mailboxer發送消息

sender = User.find(1) 
receiver = User.find(2) 
sender.send_message(receiver,"Subject", "Body") 
+0

我明白了,但我甚至無法設置發件人。這甚至不在我試圖保存的時候。這只是當我嘗試設置發件人字段(n.sender = User.find(1)將引發提到的錯誤)。我甚至無法成功實現該清單上的第一步。而且,我是通過控制檯,而不是控制器來做到這一點的。 – user3181113

+0

您正在收到此錯誤消息,因爲消息模型沒有發件人屬性。 – Monideep

+0

雖然我的應用程序或示例應用程序中沒有消息模型,但Message.create和Message.new適用於示例應用程序。 – user3181113