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)
이 새 코드로 인해 질문 테이블에 질문이 생성되지 않습니다. 데이터베이스에 제출하는 질문이 있었기 때문에 코드를 변경했지만 conversation_id
Notifications 테이블 내부에 메일 박스 젬이 생성되지 않았습니다 . 다음은 질문 테이블 내부의 질문을 만들었지 만했다 원래의 코드 NULL
에 대해 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
그래서 정의되지 않은 방법을 수정하고 질문이 conversation_id
. conversation_id
질문을받는 사람받은 편지함 (사용자가 질문에 대답하는 곳)으로 보낼 수 있도록 설정이 필요합니다 .
귀하 @conversation
가 전무하므로 변수는 아무것도 설정되지 않습니다. 이를 설정 Converation.new
하거나 데이터베이스에서 대화를 검색 하여 초기화해야 합니다 (이 경우 수행하려는 작업으로 표시됨).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다