【发布时间】:2012-11-22 21:21:47
【问题描述】:
我实际上不知道 Array ["Yes",true] 来自哪里。我有两个这样的模型:
GeneralExam has_many topic_questions
TopicQuestion belongs to general_exam, belongs_to topic
在 TopicQuestion 中,我有列:general_exam_id、topic_id、number_question,用于存储一般考试中每个主题的编号问题。
我为创建新的普通考试构建了一个嵌套表单,在表单上我构建了一个动态选择,当用户从下拉列表中选择一门课程时,我还有其他下拉列表来显示用户选择的课程主题。
这是我用于动态选择的 javascript 代码:
<% content_for :head do %>
<script type="text/javascript">
$(document).ready(function() {
$('#general_exam_course_id').change(function () {
$.ajax({
type: "POST",
url: "<%= update_topics_general_exams_path %>",
data: { course_id: $('#general_exam_course_id').val() },
dataType: "script"
});
});
</script>
<% end %>
#general_exam_course_id 是选择课程下拉列表的ID。 update_topics_general_exams_path 是我在普通考试控制器中的update 操作的路由,这是我的update 操作:
def update_topics
@course = Course.find(params[:course_id])
@topics = @course.topics
end
我还有一个update_topics.js.erb:
$('div#topic_questions select').html("<%= j options_from_collection_for_select(@topics, 'id', 'name') %>");
div#topic_questions 中的下拉列表将从update_topics 操作(我认为是这样)获得@topics 值并显示它。当我创建新的普通考试时,没关系。但是当我按下Edit 链接时,它会显示一个错误,我不知道它为什么以及它来自哪里:
NoMethodError in General_exams#edit
undefined method `name' for ["Yes", true]:Array
Extracted source (around line #3):
1: <div class="row">
2: <div class="span6"><%= remove_child_link "Remove below topic", f %></div><br><br>
3: <div class="span3"><%= f.association :topic, collection: @topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %></div>
4: <%= f.input :number_question, placeholder: 'Number of questions', label: false, style: 'display: inline' %>
5: </div>
我的edit 操作只有:@general_exam = GeneralExam.find(params[:id])。我不知道为什么topic 普通考试的关联字段显示一个 Array ["Yes", true]。我没有,在什么地方都没有定义,为什么我编辑通用考试时会出现?
更新
如果我删除 label_method: :name, value_method: :id :
<%= f.association :topic, collection: @topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
页面没有错误,但下拉列表在选择中具有值Yes, No,而不是我为考试创建的主题名称。表单可以获取每个主题的题数,但是不能获取主题的名称。
当我创建时:
当我编辑时:
我在config/locales/simple_form.en.yml:
en:
simple_form:
"yes": 'Yes'
"no": 'No'
这是个问题吗?
更新:不要在新操作中传递@topics,但它仍然存在于新页面上当尚未选择课程时
我的新动作:
def new
@general_exam = GeneralExam.new
5.times { @general_exam.topic_questions.build }
#@topics = Topic.all
end
在我上面的提取源中,我有以下新代码,也可以编辑(使用相同的形式):
<%= f.association :topic, collection: @topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
所以我没有通过@topics,但是当我转到新页面时,主题的下拉列表仍然显示Yes, No 值。
【问题讨论】:
-
如果你在 rails 控制台输入结果是什么: course = Course.find.first 然后 puts course.topics ?
-
@QumaraotBurgas 你的意思是
course = Course.first?我运行它,然后puts course.topics,它返回:#<Topic:0xb952954> #<Topic:0xbb0d9b0> => nil -
我有一种感觉,你的模型Topic没有返回一个带有方法'name'的实例对象,而是返回一个数组['Yes',true]。尝试像这样迭代控制台上的 course.topics 集合: course.topics.each do |c| (new line) puts c.name (new line) end 。或者: course.topics.each {|c|把 c.name}
-
@QumaraotBurgas 它当然返回主题名称:
Integrated Management Basic Management...。还有一件奇怪的事情,在我的新操作中,我没有@topics变量,但是当我转到新页面时,主题的下拉列表仍然显示是,否。我认为它必须出错,因为我没有通过@topics在我的新操作中,但不是。 -
对于布尔值,使用复选框。
标签: ruby-on-rails ruby-on-rails-3