2013-08-20 44 views
0

當前正在實施Threaded Messaging。 via RailsCastAncestry Gem將所有線程消息保存爲Parent

我已經安裝了Ancestry gem,但是我創建的所有消息都是父母。即)祖先=零

即使通過我的父母后:

<% for incoming in @received_messages %> 
<%= render partial: "message", locals: {incoming: incoming} %> 

_message.html.erb

ID,以新的動作

消息控制器

def index 
    @message = Message.new 
    @user = current_user 
    @sent_messages = current_user.sent_messages 
    @received_messages = current_user.received_messages 
end 

def new 
    @message = Message.new 
    @message.parent_id = params[:parent_id] 
end 

的Index.html

</div> 
    <%= link_to "Reply", new_message_path(:parent_id => incoming) ,class: "regular" %> 
    </div> 

new.ht ml.erb

<%= form_for @message do |f| %> 
    <p> 
    To:<br /> 
     <%= f.hidden_field :parent_id %> 
    <%= f.hidden_field :recipient, :value => (@message.parent.sender.name) %> 
    </p> 
    <p> 
    Message<br /> 
    <%= f.text_area :body %> 
    </p> 
    <p> 
    <%= submit_tag "Send Threaded Reply" %> 
    </p> 
<% end %> 

Message.rb

class Message < ActiveRecord::Base 

    attr_accessible :recipient, :subject, :body, :parent_id 

    has_ancestry 

end 

創建消息後,我的控制檯:

關鍵點 - 我經過PARENT_ID(144)中的郵件,但是祖先還通過如零。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"gKVr06oT+6OcAERHAzSX79vTlJLniofmEOjZPdZmfwM=", "message"=>{**"parent_id"=>"114"**, "recipient"=>"Rach Miller", "body"=>"meh and more meh"}, "commit"=>"Send Threaded Reply"} 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'Rach Miller' LIMIT 1 
    (0.2ms) BEGIN 
    SQL (0.7ms) INSERT INTO "messages" ("ancestry", "body", "created_at", "read_at", "recipient_deleted", "recipient_id", "sender_deleted", "sender_id", "subject", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["ancestry", nil], ["body", "meh and more meh"], ["created_at", Tue, 20 Aug 2013 03:14:15 UTC +00:00], ["read_at", nil], ["recipient_deleted", false], ["recipient_id", 18], ["sender_deleted", false], ["sender_id", 17], ["subject", nil], ["updated_at", Tue, 20 Aug 2013 03:14:15 UTC +00:00]] 
+1

這是weird.I'm確保您能在祖先使用PARENT_ID設置父對象。什麼是您的軌道和血統版本?您的控制器中是否有一些過濾parent_id的邏輯? – Bigxiang

+0

嘿@Bigxiang,Rails = 3.2.13和Ancestry = 2.0.0。 – RedRory

+0

我在創建函數時遇到問題,現在正在工作 – RedRory

回答

1

之後Bigxiang的評論,我決定看看我的創建功能。

我已經分別通過我的PARAMS,因此,使用

@message.parent_id = params[:message][:parent_id]工作對我來說

乾杯