未定义的方法`问题'为零:NilClass

时间:2014-04-19 00:22:42

标签: ruby-on-rails mailboxer

我收到NoMethodError undefined method `questions' for nil:NilClass。它指向行@question = @conversation.questions.build(params[:question])

上的创建操作

问题控制员:

  respond_to :js, :html

   def index
      @questions = Question.all
      respond_with(@questions)
  end

  def show
    @question = Question.find(params[:id])
    @questions = Question.order("created_at DESC")
    respond_with(@questions)
  end

  def new
    @question = Question.new
    respond_with(@question)
  end

  def create
      @question = @conversation.questions.build(params[:question])
      if @question.save
        @message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}",
                               :notification_id => @question.sender_id,
                               :receiver_id => @question.recipient_id,
                               :body => @question.question)

        @question.message = @message
        @question.save
        redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!'
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end
  end

对话控制器:

       helper_method :mailbox, :conversation
        before_filter :conversation, only: :show

     def index
        @conversations ||= current_user.mailbox.inbox.all
        end

      def reply
        current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
        redirect_to conversation
      end

      def trash_folder
        @trash ||= current_user.mailbox.trash.all 
        end

      def trash
        conversation.move_to_trash(current_user)
        redirect_to :conversations
        end

      def untrash
        conversation.untrash(current_user)
        redirect_to :conversations
        end

        def empty_trash
          current_user.mailbox.trash.each do |conversation|    conversation.receipts_for(current_user).update_all(:deleted => true)
          end
         redirect_to :conversations
        end
      end

  private

  def mailbox
   @mailbox ||= current_user.mailbox
  end

  def conversation
   @conversation ||= mailbox.conversations.find(params[:id])
  end

  def conversation_params(*keys)
   fetch_params(:conversation, *keys)
  end

  def message_params(*keys)
   fetch_params(:message, *keys)
  end

  def fetch_params(key, *subkeys)
   params[key].instance_eval do
     case subkeys.size
     when 0 then self
     when 1 then self[subkeys.first]
     else subkeys.map{|k| self[k] }
     end
   end
  end
end

消息控制器:

 def index
      redirect_to conversations_path(:box => @box)
    end

  # GET /message/new
  def new
    @message = current_user.messages.new
  end

   # POST /message/create
  def create
    @recipient = User.find(params[:user])
    current_user.send_message(@recipient, params[:body], params[:subject])
    flash[:notice] = "Message has been sent!"
    redirect_to :conversations
  end

问题模型:

  attr_accessible :answer, :question, :sender_id, :recipient_id
  belongs_to :user

  belongs_to :sender,
    :class_name => 'User',
    :foreign_key => 'sender_id'
    belongs_to :recipient,
    :class_name => 'User',
    :foreign_key => 'recipient_id'

    belongs_to :message

end

用户模型:

 acts_as_messageable
  has_many :notifications
  has_many :questions, foreign_key: :recipient_id
  has_many :sent_questions, class_name: 'Question', foreign_key: :sender_id


    def mailboxer_email(object)
        if self.no_email
          email
        else
            nil
        end
    end
end

开发日志:

Started POST "/questions" for 127.0.0.1 at 2014-05-29 12:32:46 -0400
Processing by QuestionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YWtv+TixScaYsXpJ6F47uBHkNvcruyHV7cyOtU6pWnQ=", "question"=>{"question"=>"This question should have an conversation id", "sender_id"=>"2", "recipient_id"=>"1"}, "commit"=>"Add Question"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`auth_token` = 'Mqy5_1kyb4hAsrmB9Q0fug' LIMIT 1
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `questions` (`created_at`, `question`, `recipient_id`, `sender_id`, `updated_at`) VALUES ('2014-05-29 16:32:47', 'This question should have an conversation id', 1, 2, '2014-05-29 16:32:47')
   (0.5ms)  COMMIT
WARNING: Can't mass-assign protected attributes for Message: notification_id, reciver_id
    app/controllers/questions_controller.rb:23:in `create'
    app/controllers/application_controller.rb:13:in `user_time_zone'
   (0.2ms)  BEGIN
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  SQL (0.3ms)  INSERT INTO `notifications` (`body`, `created_at`, `sender_id`, `sender_type`, `subject`, `type`, `updated_at`) VALUES ('This question should have an conversation id', '2014-05-29 16:32:47', 2, 'User', 'You have a question from 2', 'Message', '2014-05-29 16:32:47')
  SQL (0.3ms)  UPDATE `questions` SET `message_id` = 164, `updated_at` = '2014-05-29 16:32:47' WHERE `questions`.`id` = 135
   (0.5ms)  COMMIT
Redirected to http://localhost:3000/questions
Completed 302 Found in 308ms (ActiveRecord: 10.2ms)

此新代码阻止在Questions表中创建问题。我对代码进行了更改,因为我将问题提交到数据库但是没有在Notifications表中创建带有mailboxer gem的conversation_id。以下是在问题表格中创建问题的原始代码,但NULLconversation_id

def create
    @question = Question.new(params[:question])
    if @question.save
      @message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}",
                             :notification_id => @question.sender_id,
                             :reciver_id => @question.recipient_id,
                             :body => @question.question)

      @question.message = @message
      @question.save
      redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!'
    else
      render :new, alert: 'Sorry. There was a problem saving your question.'
    end
  end

因此,我需要帮助修复未定义的方法,并使用conversation_id将问题提交到数据库。我需要conversation_id设置,以便可以将问题发送到收件人收件箱(这是用户回答问题的地方)。

5 个答案:

答案 0 :(得分:5)

您的@conversation变量永远不会设置为任何内容,因此它为零。您需要将其初始化为某些内容,方法是将其设置为Converation.new或从数据库中检索对话(在这种情况下,这似乎是您要执行的操作)。

答案 1 :(得分:0)

@conversation的{​​{1}}动作中nil值为create,因为它没有设置为空。试着像这样给它

QuestionsController

希望它有所帮助!

<强>更新

我认为您应该有def create @conversation = Conversation.find(params[:id]) @question = @conversation.questions.build(params[:question]) if @question.save #scoping to the current user is the right thing to do here @message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}", #Original code :sender_id :notification_id => @question.sender_id, #Original code :recipient_id :receiver_id => @question.recipient_id, :conversation_id => @cnversation.id :body => @question.question) @question.message = @message @question.save redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!' else render :new, alert: 'Sorry. There was a problem saving your question.' end end 模型来关联Conversation模型。通过生成此查询来创建Question模型

Conversation

将关系rails g model Conversation 添加到has_many questions模型

Conversation

此外,您应该在Class Conversation < ActiveRecord::Base has_many :questions end 模型中添加belongs_to conversation

尝试更新Question模型

Question

答案 2 :(得分:0)

为什么不在模型中使用before_create回调:

#app/controllers/conversations_controller.rb
Class ConversationsController < ActiveRecord::Base
    def create
         @conversation = Conversation.new(conversation_params)
         @conversation.save
    end

    private

    def conversation_params
        params.require(:conversation).permit(:conversation, :params, questions: [])
    end
end


#app/models/conversation.rb
Class Conversation < ActiveRecord::Base
    before_create :build_questions

    private
    def build_questions
         self.questions.build
    end
end

答案 3 :(得分:0)

正如其他人所说,错误的结果是从不设置@conversation变量。

但是,我相信到目前为止没有人回答过,Conversationmailboxer gem中的模型:Mailboxer::Conversation。可以从send_message调用的结果中获得对话实例。你不得不说:

receipt = send_message ....
conversation = receipt.conversation
# Now you can save the conversation of this message 
# in some useful place so it can be found later.

其他地方的对话可以在messageable中找到。我知道这是您的User模型,显示为current_user

您可以使用

获取当前用户的所有会话
current_user.mailbox.conversations

您可以通过选择单个消息队列来执行某些过滤,例如:

current_user.mailbox.inbox

当然你可能想要最后一次谈话。为此,你想要

current_user.mailbox.conversations.last

所以我猜测你可能想要

@conversation = current_user.mailbox.conversations.last

代码中的另一个不一致之处是生成器创建的基本Mailboxer::Conversation模型没有questions关系,但您似乎假设有一个当您说

@conversation.questions

要将对话与问题联系起来,您必须使用以下内容进行修补:

class Mailboxer::Conversation < ActiveRecord::Base
  has_many :questions
end

在您自己的文件app/models/conversation.rb中。此外,仅当belongs_to :conversation模型中存在conversation_id和外键Question时才有意义。你没有。

总之,有两点意见:

  1. 您需要花一些时间通过研究其源代码来了解mailboxer的工作原理,因为文档很少。

  2. 上述模型中至少存在致命的不一致。其他人很可能。在你有成功的希望之前,你必须解决这些问题。执行此操作的方法是在转移到控制器之前测试模型。 Rails的成功就是让模型完全正确。

答案 4 :(得分:0)

从邮箱文档中,我可以看到任何邮件都有conversation_id。您声称问题的conversation_id未设置,所以我认为您的问题模型还有一个对话链接?

但我在你的问题模型中没有看到belongs_to

所以我看到了几个选项:

  • 您应该通过邮件访问对话,例如question.message.conversation
  • 要么问题的conversation_id与消息的@question相同,请执行以下操作

    @message = current_user.messages.new(...) @ question.message = @message @ question.conversation_id = @ message.conversation_id

您确实保存了@message但不保存@message?那是问题吗?

  • 您的send不是通过对话创建的,但也许您应该MessagesController,就像在{{1}}中一样:

    @message = current_user.send_message(.... @ question.update_attributes:message_id =&gt; @ message.id

希望这有帮助。