【发布时间】:2016-02-22 12:36:47
【问题描述】:
我有一个问答应用程序,用户可以在其中提出问题并获得答案。我在回答部分遇到了一些问题。
我正在尝试将问题的 ID 存储在答案表中,但是当我在控制台中检查它时它总是返回 nil。
def new
#question scaffold
if user_signed_in?
@question = current_user.questions.build
respond_with(@question)
else
redirect_to new_user_session_path
end
#answer scafold
@answer = Answer.create
@question = Question.find(params[:id])
@answer.question_id = @question
@answer.save!
redirect_to root_path
end
def edit
end
def create
#create question
@question = current_user.questions.build(question_params)
@question.save
#create answer
@answer = Answer.create(answer_params)
@question = Question.find(params[:id])
@answer.question_id = @question
@answer.save!
redirect_to :back
end
【问题讨论】:
-
你和问答表有什么关联
-
答案表可以看作是 cmets,如果它更容易的话。我已经建立了与 has_many 和 belongs_to 的关系。当我在控制台中手动添加
question_id时,它运行良好并在正确的问题中显示答案。我想要做的是将问题的 ID 存储在答案表中,而无需实际进入控制台并自己做
标签: ruby-on-rails ruby ruby-on-rails-4