Question

Currently implementing Threaded Messaging. via RailsCast

I've installed the Ancestry gem, however all messages that I create are parents. ie.) Ancestry = nil

Even after passing my parent:id to new action

Message Controller

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

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

_message.html.erb

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

new.html.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

My console after creating a message:

Key point - I'm passing the parent_id (144) in Message however ancestry still passes as nil.

 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]]
Was it helpful?

Solution

After Bigxiang's comment, I decided to take a look at my create function.

I have been passing my params individually, So using

@message.parent_id = params[:message][:parent_id] worked for me

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top