0
votes

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)

1
I don't understand, if params[:user_id] has not being passed to the controller(params[:user_id] == nil), the error will be raised. see User.find(nil)Shalev Shalit
If user_id parameter is not being passed in then you should be looking at the view which is generating this request. OTOH, if you are not sure if user_id is being passed or not, look at the logs. Pasting the logs for the new request would help.brahmana
Brahmana, added the content that you mentioned. So I tried to follow the railscast episode #262 for the ancestry gem. That's why the controller is passing the parent_id parameter. My goal here is to get the reply action to work, and be able to display all of the emails ancestry.user3785435
okay, so I played around with this a bit. Right now when 'User A' sends 'User B' a message, 'User A' becomes the sender, and has a sender_id = user_id. "User B' becomes recipient and has a recipient_id = user_id. When I hit the reply button, the reply message is not being send back to the sender, but the the recipient, so 'User B' is sending the reply to 'User B' instead of 'User A', which is wrong. Perhaps this is the problem?user3785435

1 Answers

0
votes

I'm not sure about the recipient/sender issue, but I think you just need to pass user_id as a param from your view. So the link_to would become: <%= link_to "Reply", new_message_path(:parent_id => @message, :user_id => @message.recipient_id) %>. This will make it available in the new action.