我之前问过这个问题,但搞砸了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">**
就像我说我检查了文档并且不清楚。任何指导都非常感激,因为我正在撕扯我的头发。感谢。
答案 0 :(得分:1)
<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :id, :feedback_request, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %><br>
第四个参数:feedback_request
是一个在每个反馈上调用的方法,用于确定选项名称。您可能希望将其改为:feedback_option
。
编辑:第三个参数是Feedback
上的实例方法,其值将分配给Answer#feedback_request
。因此,如果您希望将:feedback_option
字符串更改为它。
<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :feedback_option, :feedback_option, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %><br>