2016-09-17 81 views
0

我以前問過這個問題,但搞砸了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">** 

就像我說我檢查了文檔和它的不明確。任何指導非常讚賞,因爲我正在撕裂我的頭髮。謝謝。

+0

錯誤消息告訴你,你嘗試調用'feedback_request'在'Feedback'不上'Answer'。你能否加上完整的表格(不只是行)? – spickermann

+0

嗨,謝謝,我添加了我的表單... – Co2

回答

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> 
+0

好吧,非常感謝,這工作有點,但它是打印的ID而不是字符串?它在我的表格中打印「1」... – Co2

+0

用這個解決方案編輯我的答案。 –

+0

謝謝。我應該在這裏獲得一個id而不是一個字符串 - 即它應該是feedback_id而不是feedback_request?我試圖根據你的回答在活動管理員的反饋信息中進行過濾,但是當我說:feedback_id作爲第一個參數時,它會過濾掉。但我仍然想填寫feedback_request專欄......這有點鹹菜。 – Co2

相關問題