Apologies for the newbie question but, I'm getting the following error referencing message#new action in my Messages controller:
ActiveRecord::RecordNotFound (Couldn't find User without an ID): app/controllers/messages_controller.rb:18:in `new'
@recipient = User.find(params[:user_id])
I'm trying to incorporate the ancestry gem in my messages_controller, specifically, when I try to reply to a message that has been received in the user inbox(index.html.erb). I understand that there is currently no ID being passed, but does anyone encountered a similar issue? Code below:
class MessagesController < ApplicationController
def index
@messages = current_user.to_messages
end
def outbox
type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"
@messages = current_user.from_messages
end
def show
@message = Message.find params[:id]
end
def new
@message = Message.new(:parent_id => params[:parent_id])
@recipient = User.find(params[:user_id])
end
def create
@message = Message.new message_params
@message.sender_id = current_user.id
if @message.save
flash[:success] = "Your message has been sent!"
redirect_to users_path
else
flash[:failure] = "Please try again."
redirect_to users_path
end
end
def destroy
@message = Message.find(params[:id])
@message.destroy
redirect_to messages_path
end
private
def message_params
params.require(:message).permit(:content, :sender_id, :recipient_id, :parent_id)
end
end
show.html.erb (view)
From: <%= link_to @message.recipient.first_name + " " + @message.recipient.last_name, user_path(@message.recipient.id) %>,
To: <%= @message.sender.first_name + " " + @message.sender.last_name %>,
Message: <%= @message.content %>
<% if @message.recipient_id == current_user.id %>
<%= link_to "Reply", new_message_path(:parent_id => @message) %>
<% end %>
Started GET "/messages/new?parent_id=19" for 127.0.0.1 at 2014-09-27 15:41:18 -0400 Processing by MessagesController#new as HTML Parameters: {"parent_id"=>"19"} Message Load (0.1ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT 1 [["id", 19]] Completed 404 Not Found in 11ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID): app/controllers/messages_controller.rb:18:in `new'
Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms) Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms) Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms) Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.8ms)