nil的未定义方法`questions':NilClass(undefined method `questions' for nil:NilClass)

系统教程 行业动态 更新时间:2024-06-14 16:59:22
nil的未定义方法`questions':NilClass(undefined method `questions' for nil:NilClass)

我得到一个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)

这个新代码阻止了问题表中创建的问题。 我对代码进行了更改,因为我将问题提交到数据库但是没有在Notifications表中创建与mailboxer gem的conversation_id 。 下面是在Questions表中创建问题的原始代码,但对于conversation_id则为NULL 。

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以便可以将问题发送到收件人收件箱(这是用户回答问题的地方)。

I am getting a NoMethodError undefined method `questions' for nil:NilClass. It's pointing to the create action on the line @question = @conversation.questions.build(params[:question])

Questions controller:

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

Conversations controller:

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

Messages controller:

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

Questions model:

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

User model:

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

development log:

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)

This new code is preventing the question from being created in the Questions table. I made the changes to the code because I had the questions submitting to the database but it was not creating a conversation_id with the mailboxer gem inside the Notifications table. Below is the original code that created the question inside the Questions table, but had NULL for conversation_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

So I need help with fixing the undefined method and having the question submit to the database with a conversation_id. I need the conversation_id set so the Question can be sent to the recipients inbox (this is where the user answers their questions).

最满意答案

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

Your @conversation variable is never set to anything, so it is nil. You need to initialize it to something, either by setting it to Converation.new or retrieving a conversation from the database (which appears to be what you want to do in this case).

更多推荐

本文发布于:2023-04-16 19:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/972246fa834b6f4fc296066c494eb67d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   方法   nil   questions   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!