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.
Please tell me what I am doing wrong to save the user.id to the question.user_id
Everything seems fine with your view. Your problem could be in controller or model, depending on your Rails version.
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
You are posting to an incorrect route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With