我以前问过这个问题,但搞砸了 OP。这方面的官方文档不是最好的,而且有点令人困惑。我正在尝试从反馈表中收集选项并将它们呈现在表单的下拉列表中,然后使用 collection_select 将它们保存在名为“answers”的表和名为“feedback_request”的列中:
<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :id, :feedback_request, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %>
答案表:
create_table "answers", force: :cascade do |t|
t.integer "user_id", limit: 4
t.string "subject", limit: 4
t.string "source", limit: 45
t.text "description", limit: 65535
t.string "significance", limit: 45
t.string "feedback_request"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "answers", ["feedback_request"], name: "feedback_index"
add_index "answers", ["significance"], name: "signif_index"
add_index "answers", ["source"], name: "source_index"
add_index "answers", ["subject"], name: "FK_HOM_MOD_idx"
add_index "answers", ["user_id"], name: "teacher_answer_index"
反馈表
create_table "feedback", force: :cascade do |t|
t.string "feedback_option", limit: 45
end
楷模
feedback.rb
class Feedback < ActiveRecord::Base
self.table_name = 'feedback'
has_many :answers, :class_name => 'Answer'
end
answer.rb
belongs_to :feedback, :class_name => 'Feedback', :foreign_key => :feedback_request
形式:
<%= simple_form_for @answer do |f| %>
<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :id, :feedback_request, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %><br>
<%= f.text_field :Due, :id => "datepicker", placeholder: "Due", data: { no_turbolink: true }, class: "behaviourForm select large-1" %>
<%= f.input :source, :as => :text, input_html: { :style=> 'width: 100%;', :rows => 2} %>
<%= f.input :description, :label => "Comments", :as => :text, input_html: { :style=> 'width: 100%;', :rows => 2} %>
<%= f.button :submit,'Create', class: "small button buttonSelect" %>
<% end %>
这给了我在表单加载时出现以下错误:
**undefined method `feedback_request' for #<Feedback id: 1, feedback_option: "Curriculum Management">**
就像我说的,我检查了文档,但不清楚。任何指导都非常感谢,因为我正在撕扯我的头发。谢谢。