我以前問過這個問題,但搞砸了OP。官方的文件並不是最好的,有點令人困惑。我試圖從反饋表收集的選項,並使其在下拉表單上,然後將它們保存在一個名爲「答案」表和列被稱爲「feedback_request」使用collection_select:RoR:Collection_select undefined method
<%= 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">**
就像我說我檢查了文檔和它的不明確。任何指導非常讚賞,因爲我正在撕裂我的頭髮。謝謝。
錯誤消息告訴你,你嘗試調用'feedback_request'在'Feedback'不上'Answer'。你能否加上完整的表格(不只是行)? – spickermann
嗨,謝謝,我添加了我的表單... – Co2