Ruby on rails 呈现表单部分时未定义的方法
在我的问题索引页面上,我有一个所有问题的列表。我希望能够通过呈现答案表单来回答页面上的每个问题。当我尝试这样做时,会出现以下错误:Ruby on rails 呈现表单部分时未定义的方法,ruby-on-rails,Ruby On Rails,在我的问题索引页面上,我有一个所有问题的列表。我希望能够通过呈现答案表单来回答页面上的每个问题。当我尝试这样做时,会出现以下错误: undefined method `answers' for #<Question:0x00000103dc0100> 以下是视图: <% @questions.each do |question| %> <%= question.body %> <%= render :partial => "answers/
undefined method `answers' for #<Question:0x00000103dc0100>
以下是视图:
<% @questions.each do |question| %>
<%= question.body %>
<%= render :partial => "answers/form", locals: {question:question} %>
<% end %>
似乎您尚未在模型中设置关联。基本上,Rails正在您的问题模型中寻找一种称为“答案”的方法。您是否在您的答案模型中设置了
属于\u:question
,并且在您的问题模型中有\u多个:答案
?如果没有看到模型代码,就无法判断这是否是真正的问题
有关协会的更多信息:
编辑-因为您有一个has_one关联,所以语法有点不同
尝试将“answers”更改为“answer”-请参阅本文档中不同关联类型的生成方法表:似乎您尚未在模型中设置关联。基本上,Rails正在您的问题模型中寻找一种称为“答案”的方法。您是否在您的答案模型中设置了
属于\u:question
,并且在您的问题模型中有\u多个:答案
?如果没有看到模型代码,就无法判断这是否是真正的问题
有关协会的更多信息:
编辑-因为您有一个has_one关联,所以语法有点不同
尝试将“答案”更改为“答案”-请参阅本文档中不同关联类型的生成方法表:由于您有一个
所属的有一个关系,正确的关联如下:
Question.find(params[:question_id]).answer
请注意,答案
是单数。这是因为每个问题
只有一个答案
——因此问题
的实例没有方法答案
,如例外情况所示
如果您希望每个问题都有多个答案,您需要定义以下关联:
# app/models/question.rb
class Question < ActiveRecord::Base
has_many :answers
end
# app/models/answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
更新1:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
有几种方法可以将答案添加到问题
选项1:利用以下机构提供的轨道:
选项2:将答案直接分配给问题
:
answer = Answer.first
question = Question.first
question.answer = answer
在这两种方法中,请注意,由于每个问题
仅限于一个答案
,因此问题的现有答案将被替换,而不是添加到
更新2:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
以下是使用建议的每种方法时控制器操作的整体外观:
选项1:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
选项2:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
由于您有一个属于
有一个
关系,正确的关联如下:
Question.find(params[:question_id]).answer
请注意,答案
是单数。这是因为每个问题
只有一个答案
——因此问题
的实例没有方法答案
,如例外情况所示
如果您希望每个问题都有多个答案,您需要定义以下关联:
# app/models/question.rb
class Question < ActiveRecord::Base
has_many :answers
end
# app/models/answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
更新1:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
有几种方法可以将答案添加到问题
选项1:利用以下机构提供的轨道:
选项2:将答案直接分配给问题
:
answer = Answer.first
question = Question.first
question.answer = answer
在这两种方法中,请注意,由于每个问题
仅限于一个答案
,因此问题的现有答案将被替换,而不是添加到
更新2:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
以下是使用建议的每种方法时控制器操作的整体外观:
选项1:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
选项2:
# app/controllers/answers_controller.rb
def create
@question = Question.find(params[:question_id])
@question.build_answer(answer_params)
end
# app/controllers/answers_controller.rb
def create
@answer = Answer.new(answer_params)
@question = Question.find(params[:question_id])
@question.answer = @answer
end
如果您有属于问题的,并且有一个:答案,那么您只能执行以下操作:
Question.find(1).answer
不是
这将是一个有很多关系的问题
编辑
几乎肯定不是你想要的
你想要
@answer = Answer.new
或
或
如果您有属于问题的,并且有一个:答案,那么您只能执行以下操作:
Question.find(1).answer
不是
这将是一个有很多关系的问题
编辑
几乎肯定不是你想要的
你想要
@answer = Answer.new
或
或
我在答案模型中有属于:问题,在问题中有一个:答案。我在答案模型中有属于:问题,在问题中有一个:答案。好的,所以我将@answer=@question.answers.new(答案参数)改为answer.new,但现在正在使用未定义的方法new吗?(还尝试添加。在上面一行的末尾回答)我对选项一有问题。对于@question.answer.new(answer_params)行,它仍然给我同样的错误。另外,问题控制器中是否需要@answer=answer.new?返回的确切错误消息是什么@answer=answer。新的
不是特别需要的。唯一的要求是传递给关联的对象是Answer
的一个实例——不管它是新的还是现有的。现在的错误是:nil:nilclass的未定义方法“new”没有理由调用new
方法。改为使用build\u answer
——不是除了。好的,所以我将@answer=@question.answers.new(answer\u params)改为answer.new,但现在我正在使用未定义的方法new?(还尝试添加。在上面一行的末尾回答)我对选项一有问题。对于@question.answer.new(answer_params)行,它仍然给我同样的错误。另外,问题控制器中是否需要@answer=answer.new?返回的确切错误消息是什么@answer=answer。新的
不是特别需要的。唯一的要求是传递给关联的对象是Answer
的一个实例——不管它是新的还是现有的。现在的错误是:nil:nilclass的未定义方法“new”没有理由调用<