0

I want to submit a message and after that to show a message and open a popup window. The message is saved properly on DataBase, but the message don't show up , instead I receive this

NoMethodError in Private::Conversations#create
Showing /var/www/appror/app/views/private/conversations/_open.js.erb where line #3 raised:
undefined method `id' for nil:NilClass`

The application run on

Rails version             5.2.2;
Ruby version              2.5.3-p105 (x86_64-linux);
RubyGems version          2.7.6;
Rack version              2.0.6

after the message is save, in controller

/app/channels/private/conversation_channel.rb

class Private::ConversationsController < ApplicationController    
    def create
        recipient_id = Post.find(params[:post_id]).user.id
        conversation = Private::Conversation.new(sender_id: current_user.id, recipient_id: recipient_id)
        if conversation.save

            respond_to do |format|
                format.js {render partial: 'posts/show/contact_user/message_form/success'}
        end
....
end

from

/app/views/posts/show/contact_user/message_form/_success.js.erb

where it's

<%= render 'private/conversations/open' %>

it should show the message

/app/views/private/conversations/_open.js.erb

where

var conversation = $('body').find("[data-pconversation-id='" + 
                            "<%= @conversation.id %>" + 
                            "']");

Normally it should go forward and show the message and popup, but instead I'm receiving

NoMethodError in Private::Conversations#create
Showing /var/www/appror/app/views/private/conversations/_open.js.erb where line #3 raised:
undefined method `id' for nil:NilClass`

After debugging I saw that <%= @conversation %> is empty... so, it makes sense the error. But how I can pass @conversatio from controller to view ? It's possible to pass this using

respond_to do |format| format.js

?

Thank you!

EDIT:

thank you @max, indeed @conversation = .... solved the issue

Gabriela Dan
  • 61
  • 1
  • 4
  • `conversation = Private::Conversation.new(sender_id: current_user.id, recipient_id: recipient_id)` creates a local variable that is only available in the `create` method. If you want to assign an instance variable you need to use the `@conversation = ...`. You can also pass variables to the view by using [locals](https://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables). Rendering a partial from the controller is highly unconventional and quite questionable from a code organization / maintenance perspective. – max Jan 23 '19 at 19:59
  • Partials are meant to share code between views. Not to be resuable views. – max Jan 23 '19 at 20:01
  • @max you said: `Rendering a partial from the controller is highly unconventional and quite questionable from a code organization / maintenance perspective` can you please let me know why? Thank you! – Gabriela Dan Jan 24 '19 at 13:27

1 Answers1

0

You're missing an @ in your conversation variable in your controller, and your variable is falling out of scope. @conversation = Private::Conversation.new(sender_id: current_user.id, recipient_id: recipient_id

cruxite
  • 1
  • 1
  • 1