3

I have a very simple rails app to ask questions. Three Models Question, User, Teacher. Questions belong_to User and Teacher. User and Teacher has_many Questions.

I am trying to have a drop down of users and teachers to select in the Questions _form.html.erb file.

I have this collection_select and it displays fine but it will not save user id in the Questions user_id field.

<%= collection_select(:question, :user_id, User.all, :id, :name, {:prompt=>true}) %>
                       1.         2.       3.         4.   5.       6.
  1. The model
  2. input
  3. collect of users
  4. what will be saved
  5. what is displayed
  6. prompt with "please select"

Please tell me what I am doing wrong to save the user.id to the question.user_id

sites
  • 21,417
  • 17
  • 87
  • 146
Tyler
  • 678
  • 7
  • 22

1 Answers1

3

Everything seems fine with your view. Your problem could be in controller or model, depending on your Rails version.

My first guess

In Rails 3:

class Question < ActiveRecord::Base
  attr_accessible :user_id

In Rails 4:

class QuestionsController <  ApplicationController
  def question_params
    params.require(:question).permit(:user_id)
  end

Second guess

You are posting to an incorrect route.

sites
  • 21,417
  • 17
  • 87
  • 146