0

I previously ask this question but messed up the OP. The official docs for this are not the best and a little confusing. I am trying to gather options from a feedback table and render them in a dropdown on a form and then save them in a table called "answers" and column called "feedback_request" using collection_select:

<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :id, :feedback_request, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %>

Answers table:

  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"

Feedback table

create_table "feedback", force: :cascade do |t|
    t.string "feedback_option", limit: 45
  end

Models

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

Form:

<%= 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 %>

This gives me the following error on form load:

**undefined method `feedback_request' for #<Feedback id: 1, feedback_option: "Curriculum Management">**

Like I say I checked the docs and it's not clear. Any guidance much appreciated as i'm tearing my hair out. Thanks.

Co2
  • 343
  • 1
  • 14

1 Answers1

1
<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :id, :feedback_request, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %><br>

The fourth argument :feedback_request is a method that is called on each of your feedbacks to determine the option name. You probably want this to be :feedback_option instead.

Edit: The third argument is the instance method on Feedback whose value will be assigned to Answer#feedback_request. So if you want that to be the :feedback_option string change it.

<%= f.collection_select :feedback_request, Feedback.order(:feedback_option), :feedback_option, :feedback_option, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %><br>
Jack Noble
  • 2,018
  • 14
  • 12
  • Ok, thanks very much, that works sort of but it's printing the id rather than the string?? It's printing "1" in my table... – Co2 Sep 17 '16 at 16:56
  • Edited my answer with the solution to that. – Jack Noble Sep 17 '16 at 17:45
  • Thanks. Should I be getting an id here instead of a string - ie should it be feedback_id instead of feedback_request? I tried to filter in active admin on feedback based on your answer and nothing comes up, but when I say :feedback_id as first argument instead then it filters. But I still want to fill the feedback_request column...that's a bit of a pickle. – Co2 Sep 17 '16 at 17:49
  • Yeah, `:feedback_id` and `:id` is how I would do it. Do you really need to fill in `feedback_request`? You can just get that information from the feedback. If you do you can add code that handles that in the model. – Jack Noble Sep 17 '16 at 17:58
  • I think it's best to go with feedback_id as I just want to be sure on the integrity of the data. <%= f.collection_select :feedback_id, Feedback.order(:feedback_option), :id, :feedback_option, {prompt: "Feedback"}, {class: "select large-1 behaviourForm"} %> So i'm doing this. Problem is nothing is showing up, although the good thing is the filter I mentioned works. Thanks for your help, any tip on defining the labeling in the model? Cheers. – Co2 Sep 17 '16 at 18:05